javascript - What is cURL '-F' param doing in terms of node.js -
i'm trying copy curl command in node.js. command this:
curl https://search.craftar.net/v1/search -f "token=xxx" -f "image=@someimage.jpg"
this fine, how translate node.js? using request library:
request({ url: 'https://search.craftar.net/v1/search', method: 'post', form: {token: 'xxx', image: binarybodyofprevrequest}, headers: {'content-type': 'multipart/form-data'} }, function(err, res, body) { console.log(body) // prints out: {"error": {"message": "reference image required", "code": "image_missing"}} });
it seems token being recognised image not. why that?
i've looked @ facebook api - " curl -f "?, , see form. don't know how compare idea of html form vs multipart/form-data, or form maybe in terms of data sending.
instead of manually setting content-type , using form
, use formdata
instead of form
in request()
options. send multipart/form-data request instead of application/x-www-form-urlencoded request. reason need use multipart/form-data is request type used transfer raw binary data.
additionally, if image local (or if can obtain image via stream of other kind), can pass stream file instead of literal contents. can memory usage since won't have buffer entire file first. example:
formdata: { token: 'foo', image: fs.createreadstream('/path/to/image.jpg') }
Comments
Post a Comment