I try to make an HTTP PUT to an API. this endpoint allow sending a file and it saves it somewhere for later retrieval.
Here is the way I do it (successfully) with curl:
curl -sSf -T file1
http://api.my-server/file1 (PUT) and curl -O -L
http://api.my-server/file1 (GET)
Here is a verbose PUT: (the unique name of my file will be test-file-7)
curl -sSf -T file1
http://api.my-server.com:3000/test.bla/test-file-7 -v
> PUT /test.bla/test-file-7 HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host:
api.my-server.com:3000> Accept: */*
> Content-Length: 12
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Server: nginx/0.6.34
< Date: Tue, 24 Jul 2012 02:10:18 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: close
< Vary: Accept-Encoding
< Status: 200 OK
<
* Closing connection #0
here is my node code that is trying to imitate the curl:
module.exports = saveFile;
var request = require('request'); // Mikeal's request package
var rand = Math.floor(Math.random()*100000000).toString();
// curl -sSf -T file1 <url>/file-name
function saveFile() {
request({
method: 'PUT',
uri: '
http://api.my-server.com:3000/file-' + rand,
multipart: [ {
'content-type': 'application/json',
body: JSON.stringify({
foo: 'bar',
_attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}
})
},
{ body: 'I am an attachment' }
]
}
, function (error, response, body) {
if(response.statusCode == 200){
console.log('ok);
} else {
console.log('error: '+ response.statusCode)
console.log(body)
}
});
};
the difference between this code and the curl version is i am not loading a file from the file system in the node version.
if this is the issue, can anyone guide me about doing it? i probably need to change the content type and might not need the multipart? i am not sure.
here is the output i see in the terminal:
// boundary >> D0F5DF48-E3F9-4C8B-93F2-2A7EFA47F7C4
// ok
// when i retrieve the file from the server using curl -O -L
http://api.my-server/file1// i see:
cat file1 =>
--D0F5DF48-E3F9-4C8B-93F2-2A7EFA47F7C4
content-type: application/json
{"foo":"bar","_attachments":{"message.txt":{"follows":true,"length":18,"content_type":"text/plain"}}}
--D0F5DF48-E3F9-4C8B-93F2-2A7EFA47F7C4
I am an attachment
any idea why do i see so many lines instead of only the last one?
is it the multipart or maybe the content-type?
Also, I can talk to the guy that created the API i am using. i am just not sure what questions should I ask him.
Also, any insight about mutiparts, mime-types and encoding would be appreciated. this stuff is confusing!
Thanks!