1

I have this line with curl and it's working fine:

curl -X POST -F document=@name_of_document_with_json_inside.txt http://some_url/json/name?signature=bd87b1e4b679092a6946de0c6f623567

I want to change this and POST my json document with nodejs

My current code is:

var request = require('request');
var postData = { id: "12345", title: "Some title", url: "some-url" };
var clientServerOptions = {
  uri: 'http://some_url/json/name?signature=bd87b1e4b679092a6946de0c6f623567',
  body: postData,
  json: true,
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  }
}

request(clientServerOptions, function (error, response) {
  console.log(error,response.body);
  return;
});

From curl help i got that -F is for forms and in my curl i have a command that is posting field document:

-F/--form <name=content> Specify HTTP multipart POST data (H)

Can anyone help me out how to specify document field and post my json data in it?


Update:

postData contains:

{ id: "12345", title: "Some title", url: "some-url" }

This is the data that is in name_of_document_with_json_inside.txt

I was told that i need to do it something like posting textarea with name=document:

<textarea name="document">JSON BLA BLA BLA</textarea>

So how to specify document= ?

  • Are you trying to upload a file? – Master Po May 17 '17 at 19:34
  • Also try with `var clientServerOptions = { method: 'POST', url: 'http://some_url/json/name', qs: { signature: 'bd87b1e4b679092a6946de0c6f623567' }, headers: { 'content-type': 'multipart/form-data;' }, formData: { document: 'name_of_document_with_json_inside.txt' } };`. Check [this link](https://github.com/request/request#forms) too. – Master Po May 17 '17 at 19:41
  • I'm sending json string { id: "12345", title: "Some title", url: "some-url" } this is in the .txt file, now i want to send this string like a post form with field name document. something like i post textarea with name=document and json string in it – Nikola Stojanoski May 19 '17 at 18:16
  • You should send the data to the API in the format it defined. Does the API accepts content type `application/json` ? Or we should sent the content as `formdata`. Can you share the code that implements the API? – Master Po May 19 '17 at 19:26

1 Answers1

0

This post helped me out: http://stackoverflow.com/questions/19818918/nodejs-sending-uploading-a-local-file-to-a-remote-server

var POST_DATA = 'data={[' was changed to: var POST_DATA = 'document={['

I dodn't even have to write to file and post postData json code

Here is full working code:

var http = require('http');
var postData = { id: "12345", title: "Some title", url: "some-url" };
var post_options = {
    //host: 'logger.mysite.co.uk',
    host: 'some_url',
    path: '/json/name?signature=bd87b1e4b679092a6946de0c6f623567',
    port: 80,
    timeout: 120000,
    method: 'POST',
}

var sender = http.request(post_options, function(res) {
    if (res.statusCode < 399) {
        var text = ""
        res.on('data', function(chunk) {
            text += chunk
        })
        res.on('end', function(data) {
            console.log(text);
        })
    } else {
        console.log("ERROR", res.statusCode)
    }
});

var POST_DATA = 'document='
POST_DATA += postData
POST_DATA += ''
sender.write(POST_DATA);
sender.end();