Skip to content

Instantly share code, notes, and snippets.

@cosmicflame
Created May 9, 2014 21:49
Show Gist options
  • Save cosmicflame/dd4bf009f6f7ababaa54 to your computer and use it in GitHub Desktop.
Save cosmicflame/dd4bf009f6f7ababaa54 to your computer and use it in GitHub Desktop.
unirest-nodejs .attach() path issues on Windows
var unirest = require('unirest')
var path = 'C:\\Temp\\test-data.txt'
unirest.post('http://httpbin.org/post')
.attach('my-file', path)
.end(function(res) {
console.log(res.body)
})
$ node gist.js
stream.js:94
throw er; // Unhandled stream error in pipe.
^
Error: ENOENT, open 'c:\dev\temp\unirest-issue\node_modules\unirest\C:\Temp\test-data.txt'
var unirest = require('unirest')
var path = 'test-data.txt'
unirest.post('http://httpbin.org/post')
.attach('my-file', path)
.end(function(res) {
console.log(res.body)
})
$ node gist.js
stream.js:94
throw er; // Unhandled stream error in pipe.
^
Error: ENOENT, open 'c:\dev\temp\unirest-issue\node_modules\unirest\test-data.txt'
var unirest = require('unirest')
var path = 'C://Temp/test-data.txt'
unirest.post('http://httpbin.org/post')
.attach('my-file', path)
.end(function(res) {
console.log(res.body)
})
$ node gist.js
Path with forward slashes
{ data: '',
json: null,
headers:
{ 'X-Bluecoat-Via': '(removed)',
Connection: 'close',
'Content-Type': 'multipart/form-data; boundary=--------------------------14
8007494553476287118800',
Host: 'httpbin.org',
'Transfer-Encoding': 'chunked',
'X-Request-Id': 'b27077cb-c304-459c-9cfb-4f18ddca466e' },
args: {},
url: 'http://httpbin.org/post',
origin: '(removed)',
files: {},
form: {} }
(Despite "files: {}", watching the request with Fiddler showed the file being sent. It doesn't show in requestb.in either. Side note!)
var unirest = require('unirest')
var path = require('path')
var relPath = path.relative('./node_modules/unirest', 'C:\\Temp\\test-data.txt')
console.log("Path we're going to use", relPath)
unirest.post('http://httpbin.org/post')
.attach('my-file', relPath)
.end(function(res) {
console.log(res.body)
})
As paths are treated as relative to __dirname in Unirest, the input path needs to be modified to be relative to where unirest is installed. This feels rather like a terrible hack...
$ node gist.js
Path we're going to use ..\..\..\..\..\Temp\test-data.txt
Response
{ data: '',
origin: '15.219.169.71',
json: null,
form: {},
url: 'http://httpbin.org/post',
args: {},
files: {},
headers:
{ 'Transfer-Encoding': 'chunked',
'X-Bluecoat-Via': 'a9f78fc983ea0416',
'Content-Type': 'multipart/form-data; boundary=--------------------------28
0340137059443056370490',
Connection: 'close',
Host: 'httpbin.org',
'X-Request-Id': '20dda8a6-6782-4c48-b5aa-8d0b6a0f53f3' } }
I'm trying to upload a file using unirest-nodejs and an HTTP POST. The documentation states that:
"If path contains http or https Request will handle it as a remote file, otherwise if it contains :// and not http or https it will consider it to be a direct file path. If no :// is found it will be considered a relative file path."
This doesn't seem to work for Windows (and I question whether it works for Linux either).
- If the path has http:// or https:// then we're fine (not demonstrated here)
- If the path is an absolute Windows path (e.g. C:\\Temp\\test-data.txt) then it's treated as relative to ./node_modules/unirest (see example 1)
- If the path is a relative path (e.g. test-data.txt) then it's treated as relative to ./node_modules/unirest (see example 2)
- If the path has ://, e.g C://Temp/test-data.txt (see example 3) then the request is made.
This is rather awkward for Windows users, as file paths don't have '://' in them.
@cosmicflame
Copy link
Author

Issue filed with unirest-node: Kong/unirest-nodejs#12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment