-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
buffer file uploads work but streams do not #197
Comments
Thanks for the heads up @bchr02. I'll take a look in a minute and let you know what I find. What version of Node are you running this in? |
6.9.4 LTS 64bit Windows
Thanks
|
I think it has to do with the receiving side. I just tested the following code and it works without a problem: var http = require('http'),
needle = require('..'),
fs = require('fs');
http.createServer(function(req, res) {
var ws = fs.createWriteStream('./out');
ws.on('close', function() {
res.end('Thanks.');
console.log('File size:', fs.statSync('./out').size);
});
req.pipe(ws);
}).listen(5000);
var options = {
multipart: false,
headers: {
'Content-Type': 'application/pdf;name=test.pdf',
}
}
var stream = fs.createReadStream(__dirname + '/test.pdf');
needle.post('localhost:5000', stream, options, function(err, resp, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
var buffer = fs.readFileSync(__dirname + '/test.pdf');
needle.post('localhost:5000', buffer, options, function(err, resp, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
}); What kind of server is processing the request? |
Not exactly sure. It's a proprietary image server. Our ERP vendor uses it to store images from their ERP client software. The software that I am writing needs to be able to leverage off this proprietary image server and send requests the same way as the ERP client software does. I inspected the HTTP communication that the ERP client software is sending and have successfully been able to utilize the image server using Buffers but not using Streams. From an abstraction stand point I am not sure why there should be any difference when using Buffers over Streams? |
I was able to successfully run your test script. If in fact it is the receiving server that is causing the issue that would have to mean that needle is not sending the POST in exactly the same way, no? |
and if that's the case perhaps there is an issue with needle or one if's dependencies. |
actually I believe node's http module sends file streams as chunks and not within the payload. Therefore the issue would be with the receiveing server, like you said. 😄 |
The strange thing is that using Unirest I was able to get it working with Streams however for some reason Unirest would change the content-type so I opened Kong/unirest-nodejs#102 and then came across needle. |
Since the error you're getting is a |
nope that didn't work. But thanks for the suggestion. I forgot to mention that while using Streams although I get the socket hang error everything else works. In other words the image server successfully copies the file to the where it needs to go and does the few other tasks that it does. The only issue is that the body of needle's callback response is supposed to provide the image key which I need but instead I get the error. I even tried removing the the error to see if maybe the body would contain the response but it does not. |
I started searching around the request issues and can see many others are having similar errors. One person in particular recommended using bhttp which he explained "uses streams2". To my amazement I gave bhttp a try and it worked perfectly. Here is the code: var fs = require("fs");
var bhttp = require("bhttp");
var stream = fs.createReadStream(__dirname + '/code128.pdf');
var options = {
multipart: false,
headers: {
'content-type': 'application/pdf;name=code128.pdf'
}
};
bhttp.post("http://192.168.1.200:95/Insert", stream, options, function(err, response) {
console.log("Response from server:", response.body.toString());
}); Regardless, I want to thank you for your help. I greatly appreciate it. You can close this issue unless you would like to try to continue to resolve it at which point I wouldn't mind testing it out for you. Let me know. |
Sure @bchr02, always glad to help (if I can). It's hard for me to debug the issue without being able to reproduce it locally. I even tried the example code in the My guess is that this has something to do either with the 'content-length' header or the 'content-transfer-encoding' one. It looks like the server is closing the request before the client is done sending the data, so by setting one of these two headers we should be able to prevent that behaviour. If possible, try running your code with these options:
Or by prefetching the length of the stream (it's not what you would normally do, but just to see whether this is the problem or not):
And let me know how it goes! |
Adding |
That's interesting. So setting the |
that's correct.
|
@tomas Thank you! |
In the below examples, buffer file uploads work but streams do not. In the streams example I get a
Error: socket hang up
error.Because of the third party API in which I am uploading to, the headers must be sent as outlined in the examples, however I do not believe this to be causing the issue. My hunch is that the read stream is not ending. Any help would greatly be appreciated. Thank you.
Here is the Stream version:
Here is the Buffer version:
The text was updated successfully, but these errors were encountered: