2

postman request

how can I post a file(please refer my screenshot) with unirest in node.js. I have gone through unirest doc it's found that can use the below code for sending form-data to a given URL

unirest.post('http://mockbin.com/request')
.headers({'Content-Type': 'multipart/form-data'})
 .field('parameter', 'value') // Form field
.attach('file', '/tmp/file') // Attachment
.end(function (response) {
 console.log(response.body);
});

please have a look at the screenshot attached. needed to give the key name as 'html'.

how to export the same postman request to node.js(unirest)

basith
  • 740
  • 4
  • 13
  • 26

1 Answers1

2

In .attach('file', '/tmp/file'), first argument is field name(key name according to you) and second is file path, you can pass as following

var unirest = require('unirest');

unirest.post('http://localhost:3000/api/addProject/')
.headers({'Content-Type': 'multipart/form-data'})
.attach('html', 'D:\\data\\index.html') // Attachment
.end(function (response) {
  console.log(response.body);
});
Arif Khan
  • 5,039
  • 2
  • 16
  • 27
  • '.field('parameter', 'value') // Form field' why are we adding this to our code? – basith May 29 '18 at 12:38
  • you may remove if not required, that is only example to pass data with file, you can remove that line – Arif Khan May 29 '18 at 12:42
  • the issue is fixed. I was trying to send the file itself on the .attach section. worked when I have given the s3 URL of the same file – basith May 30 '18 at 03:07