admin管理员组

文章数量:1394544

I am using "hackathon-starter" node bunch for my project. In this build when I try to call a API from request.post it will take "Content type 'application/x-www-form-urlencoded;charset=utf-8' header for all API. I have tried to change header from API calling but it will take only

Content type : 'application/x-www-form-urlencoded;charset=utf-8'

header for all API. I have tried below code. I want to set application/json for all API.

var querystring = require('querystring');
      var request     = require('request');

      var form = {
        "userType": req.body.type,
        "userName": req.body.mobile,
        "email": req.body.email,
        "name": req.body.name,      
        "password": req.body.password
      };  

      var formData = querystring.stringify(form);
      var contentLength = formData.length;
      request.post({
          headers: {'content-type':'application/json'},
          url:'mylink',
          form:    formData // I have tried form as well.
      },function(error, response, body){
      console.log(body)
    });

My error message on console.

{"timestamp":1484822264270,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/x-www-form-urlencoded;charset=utf-8' not supported","path":"mylink"}

I am using "hackathon-starter" node bunch for my project. In this build when I try to call a API from request.post it will take "Content type 'application/x-www-form-urlencoded;charset=utf-8' header for all API. I have tried to change header from API calling but it will take only

Content type : 'application/x-www-form-urlencoded;charset=utf-8'

header for all API. I have tried below code. I want to set application/json for all API.

var querystring = require('querystring');
      var request     = require('request');

      var form = {
        "userType": req.body.type,
        "userName": req.body.mobile,
        "email": req.body.email,
        "name": req.body.name,      
        "password": req.body.password
      };  

      var formData = querystring.stringify(form);
      var contentLength = formData.length;
      request.post({
          headers: {'content-type':'application/json'},
          url:'mylink',
          form:    formData // I have tried form as well.
      },function(error, response, body){
      console.log(body)
    });

My error message on console.

{"timestamp":1484822264270,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/x-www-form-urlencoded;charset=utf-8' not supported","path":"mylink"}

Share Improve this question edited Dec 9, 2023 at 14:15 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jan 19, 2017 at 10:46 Pritesh MahajanPritesh Mahajan 5,1749 gold badges43 silver badges65 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

I guess you need to use json option instead based on your requirements:

  var form = {
    "userType": req.body.type,
    "userName": req.body.mobile,
    "email": req.body.email,
    "name": req.body.name,      
    "password": req.body.password
  };  

  request.post({
      url:'mylink',
      json: form,
  },function(error, response, body){
  console.log(body)
});

From the options documentaion:

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

本文标签: javascriptSet contenttype header to json for requestpostStack Overflow