1

for example: to transfer a obj to /localhost/transfer

var request = require('request');
var obj={ 
  tag: 'false',
  array: [ '3', '4', '5'] 
}

request.post({url:/localhost/transfer, form:obj},function(err,httpResponse,body){
            if(err){
                res.json({error:false});
            }else{
                res.json({error:true});
            }
        });

get obj

router.post('/transfer',function(req, res, next){
  console.log(req.obj);

});

the console obj display:

{ tag: 'false',
  'array[0]': '3',
  'array[1]': '4',
  'array[2]': '5' }

How to send or receive data correctly? like this

 { 
      tag: 'false',
      array: [ '3', '4', '5'] 
    }

request: https://github.com/request/request#custom-http-headers

3 Answers 3

2

The /transfer endpoint receives obj as 'array[0]': '3', 'array[1]': '4', 'array[2]': '5' because the Content-Type of the HTTP request is application/x-www-form-urlencoded -- in the request.post() option, form property is used.

In order to send JSON object to /transfer, the Content-Type should be application/json. In module request, it can be implemented as below:

var request = require('request');

var obj={
  tag: 'false',
  array: [ '3', '4', '5']
};

// requestb.in URL is used here for example, but it can be replaced.
request({
  url: 'https://requestb.in/srp1bmsr',
  method: 'post',
  body: obj,
  json: true
}, function(err, response, body) {
  if(err){
    // deal with error.
  }else{
    // deal with success response.
  }
});

According to module request's document, body and json in the code means:

body - entity body for PATCH, POST and PUT requests. Must be a Buffer, String or ReadStream. If json is true, then body must be a JSON-serializable object.

json - sets body to JSON representation of value and adds Content-type: application/json header. Additionally, parses the response body as JSON.

Sign up to request clarification or add additional context in comments.

2 Comments

Why does not the document mention this part?
@AlbertChen Do you mean application/x-www-form-urlencoded? Actually, the request module document mentioned by providing a simple example. Please search "application/x-www-form-urlencoded (URL-Encoded Forms)" in the document page and you will get it.
1

You can set headers using the headers property.

request.post({
   url:/localhost/transfer,
   body: obj,
   json: true
},function(err,httpResponse,body){
    if(err){
        res.json({error:false});
    }else{
        res.json({error:true});
    }
});

You can also set json: true, then it will add the header automatically and also parse the response body as JSON.

1 Comment

@AlbertChen It's not working because form property is used in request.post() and body is missing. Also, please note it is not necessary to define "content-type": "application/json" here as json: true will add it automatically.
0

When sending you have to set JSON headers as the content type.

Send

let message = {
    tag: 'false',
    array: [ '3', '4', '5'] 
};

return this.http
      .post(url, message, new Headers({'Content-Type': 'application/json'}))     

When receiving you have to use the JSON body parser to parse the req body. https://www.npmjs.com/package/body-parser

Receive

router.post('/signin', function (req, res) {
    console.log(req.bod);           
});

Here I'm using express as my node server. In your app.js (server.js or whatever main script) set the body parser to parse JSON payloads.

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());

3 Comments

github.com/request/request#custom-http-headers,Forgot to mention that I am using this kit, which seems to have similar functionality, but I do not know how to use
It's for client side right? What are you using for server side?
Do you redirect the request to another server? Ok. Now what is your problem? Didn't my answer help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.