node-js-libs / curlrequest

A cURL wrapper
MIT License
184 stars 44 forks source link

upload file #20

Closed thomasmodeneis closed 10 years ago

thomasmodeneis commented 10 years ago

Hi chriso, I like your lib, how can I use to for file upload ?

Like: curl -i -F name=test -F filedata=@localfile.jpg http://example.org/upload Thank you

chriso commented 10 years ago

The following should work:

var curl = require('curlrequest');

var options = {
    url: 'http://example.org/upload',
    form: 'name=test&filedata=@localfile.jpg'
};

curl(options, function (err, response) {
    // ...
});

Usually you would pass in an object for the form option such as {name: 'test', filedata: '@localfile.jpg'} but this causes the @ symbol to be escaped.

chriso commented 10 years ago

You can see the options that are passed to curl by inspecting the meta argument that is passed to the callback:

var options = {
    url: 'http://example.org/upload',
    form: 'filedata=@localfile.jpg',
    pretend: true
};

curl.request(options, function (err, data, meta) {
    console.log(meta.args);
});

The pretend option tells the lib not to make the actual request which may be useful during debugging.

hugoliv commented 9 years ago

Thank chriso for your answer. My question is how to manage digest auth and upload file.

This is not working:

var options = { url: url, form: 'image_url='+image_url, method: 'PUT', digest: true, user: 'XXXXX', password: 'xxxxxx', verbose: true };

nor: var options = { url: url, form: 'image_url='+image_url, method: 'PUT', digest: {user: 'xxxx', password: 'xxxXXxxx'}, verbose: true };

log: URL malformed. The syntax was not correct

digest: 'xxxxx:XXXXXX' return an error: Couldn't resolve host. The given remote host was not resolved.

hugoliv commented 9 years ago

if someone is interested in: var id = 'xxxxxxx'; var psw = 'XXXXXXX'; var user = id + ':' + psw;

var options = { url: url, form: 'filedata=@localfile.jpg', method: 'PUT',//case sensitive proxy: '',// -X digest: true,//enable digest auth user: user, verbose: true };

Sk8er22 commented 9 years ago

Hi!

i need this curl curl -X POST -H "Content-Type: multipart/form-data" -F "file=@./filepath.jpg" -F"name_discoteca=Testing" -F "id_discoteca=19" -F "ispublic=1" -F "id_user=asd" WEBURL << this works...

but i try with your library a lot of combinations using --form & --data with " ' ; & , for join all the parameters but not work

Can you explain me pls? i need POST send a file and 4 parameters

chriso commented 9 years ago

@Sk8er22 try:

var options = {
    method: 'POST',
    headers: {'Content-Type': 'multipart/form-data'},
    form: 'file=@./filepath.jpg&name_discoteca=Testing&id_discoteca=19&ispublic=1&id_user=asd'
    url: 'http://example.org/upload'
};

curl.request(options, function (err, data) {
    ....
});
Sk8er22 commented 9 years ago

nope, form with & and = with or without double quotes doesn't works :( ask for parameters

chriso commented 9 years ago

When you say doesn't work, what's actually happening? Are you getting an error, is nothing happening, or is the request being made but not with the file appended?

A few things to try:

Sk8er22 commented 9 years ago

When i do what you said, the php doesn't got the file but got the parameters... the only way that works is like i said with -F i add behind url comand all my -F -F -F -F this can works?

i get this printing command:

now i have this in log : curl --silent --show-error --no-buffer --url http:url.com -F "file=@/Users/user/Downloads/WiPho-master/public/previews/dscn0271.jpg" -F "name_discoteca=Testing" -F "id_discoteca=19" -F "ispublic=1" -F "iduser=ads" --request POST --location --max-redirs 3 --header Accept: /_ --header Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 --header Accept-Language: en-US,en;q=0.8

if i copy and paste it works! but on the JS dosen't do anything T_T

Sk8er22 commented 9 years ago

i have this error node_modules/curlrequest/index.js:223 options.header.push(key + ': ' + headers[key]); when i use headers than u suggest, and if i only use --data all the parameters OK but the file it's not upload

Thanks for help!!!!!!!!!!

chriso commented 9 years ago

Yeah it looks like the reason that the file isn't making it is because it's missing the Content-Type: multipart/form-data header.

What version of curlrequest are you using?

Try passing in headers: {'content-type': 'multipart/form-data'} instead.

Sk8er22 commented 9 years ago

works that header, but now i miss the parameters xD

[ '--silent', '--show-error', '--no-buffer', '--url', 'http://URL', '--data', 'id_user=123&name_discoteca=Testing&iddiscoteca=19&ispublic=1&file=@/Users/user/Downloads/WiPho-master/public/previews/dscn0278.jpg', '--request', 'POST', '--location', '--max-redirs', 3, '--header', 'Accept: /_', '--header', 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3', '--header', 'Accept-Language: en-US,en;q=0.8', '--header', 'Content-Type: multipart/form-data', '--user-agent', 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/5.0)' ]

And if i don't sent the headers detects the parameters but not the file... :S

File not recibed [ '--silent', '--show-error', '--no-buffer', '--url', 'http://URL', '--data',

'id_user=123&name_discoteca=Testing&iddiscoteca=19&ispublic=1&file=@/Users/user/Downloads/WiPho-master/public/previews/dscn0278.jpg', '--request', 'POST', '--location', '--max-redirs', 3, '--header', 'Accept: /_', '--header', 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3', '--header', 'Accept-Language: en-US,en;q=0.8', '--user-agent', 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.861.0 Safari/535.2' ]

chriso commented 9 years ago

Yeah you'll need to use form rather than data.

Try passing in an array:

form: ['file=@./filepath.jpg', 'name_discoteca=Testing', 'id_discoteca=19', 'ispublic=1', 'id_user=asd']
Sk8er22 commented 9 years ago

on curl call --form [object Object] if i set an array

and if i set everything in a line with & or ; joining them doesn't work, miss parameters... the call have to call for each object a -F ... something crazy... i don't know why

i try calling a form for each one but just write the last one, it's possible than write one for each?

chriso commented 9 years ago

Ok I've fixed the issue in version 0.5.2. Update curlrequest and then try again with passing the form vars via an array.

Sk8er22 commented 9 years ago

that's true, anyway u join the array with & and not work... just take the first parameter... --form i guess than need a --form for each one not join all parameters =/

curl --silent --show-error --no-buffer --url http://asd/asd.php --form file=@/Users/asd/Downloads/WiPho-master/public/previews/dscn0348.jpg&id_user=123&name_discoteca=Testing&iddiscoteca=19&ispublic=1 --request POST --location --max-redirs 3 --header Accept: /_ --header Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 --header Accept-Language: en-US,en;q=0.8 --header Content-Type: multipart/form-data --user-agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_6) AppleWebKit/534.22 (KHTML, like Gecko) Chrome/11.0.683.0 Safari/534.22

chriso commented 9 years ago

I you upgrade to 0.5.2 and then pass in the form vars like so:

var options = {
    method: 'POST',
    headers: {'content-type': 'multipart/form-data'},
    form: [
        'file=@./filepath.jpg',
        'name_discoteca=Testing',
        'id_discoteca=19',
        'ispublic=1',
        'id_user=asd'
    ],
    url: 'http://example.org/upload'
};

curl.request(options, function (err, data) {
    ....
});

It will correctly split them up as separate args to curl:

curl ... --form "file=@./filepath.jpg" --form "name_discoteca=Testing" --form "id_discoteca=19" ...
Sk8er22 commented 9 years ago

I love you! i'm too tard! hahahha I owe you a beers... WORKS

chriso commented 9 years ago

Great :smile:

muton commented 6 years ago

@chriso Thanks for doing this library, would be handy to see the form array syntax in the readme - took me a while to track it down here!