【问题标题】:how to set content type in "request" npm package如何在“请求”npm 包中设置内容类型
【发布时间】:2018-05-26 02:05:02
【问题描述】:

我正在从节点服务器向其他服务器发送请求,但我需要发送内容类型

应用程序/json

如何发送,我正在使用这种格式

request.post('https://server.com/index.php/rest/V1/integration/admin/token',{form:postData},function (error, response, body) {
    console.log('error:', error); // Print the error if one occurred
    console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
    console.log('body:', body); // Print the HTML for the Google homepage.
    res.json({
        'error': error,
        'statusCode': response && response.statusCode,
        'body': body
    })
});

我在尝试这个时遇到错误

 request.post(
        'https://server.com/index.php/rest/V1/integration/admin/token',
        {
            form:postData,
            headers:{ 
                "Content-Type": "application/json"
            }

    },function (error, response, body) {
        console.log('error:', error); // Print the error if one occurred
        console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
        console.log('body:', body); // Print the HTML for the Google homepage.
        res.json({
            'error': error,
            'statusCode': response && response.statusCode,
            'body': body
        })
    });

"message":"服务器无法理解 Content-Type HTTP 标头媒体 输入 application/x-www-form-urlencoded"

【问题讨论】:

  • 文档中有一些内容类型的示例:github.com/request/request
  • 当你提供一个key json它会自动设置内容类型application/json
  • @ManjeetThakur 但我收到此错误“服务器无法理解 Content-Type HTTP 标头媒体类型应用程序/x-www-form-urlencoded”,如果我设置,我可以从邮递员发布数据应用程序/json
  • 可以发一下邮递员的图片你是怎么用的吗

标签: javascript node.js request


【解决方案1】:

请将密钥名称 form 更改为 json

    request.post('https://server.com/index.php/rest/V1/integration/admin/token', {
    json: postData
}, function(error, response, body) {
    console.log('error:', error); // Print the error if one occurred
    console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
    console.log('body:', body); // Print the HTML for the Google homepage.
    res.json({
        'error': error,
        'statusCode': response && response.statusCode,
        'body': body
    })
});

【讨论】:

  • 标头设置在哪里?
  • @Ash 你能告诉我投反对票的原因是什么吗?
  • 啊,对不起,我看不到您已将表单更改为 json 并且您也没有提及这一点,这应该是可以接受的答案。我正在改变它。
  • @Ash 谢谢。
  • 请编辑您的答案,然后我才能更改我的投票,最好在文本中提及我需要更改的内容
【解决方案2】:

根据the docs,您需要设置json:true 以将application/json 添加到标题中

let data = { foo: "bar" }

request.post({
    uri: 'www.domain.com/upload',
    body: data,
    json:true
}, function (err: any, res: any, body: any) {
    //handle callback            
});

【讨论】:

    【解决方案3】:

    由此修复,

    request.post({
        url: url,
        method: "POST",
        headers: {
            "content-type": "application/json",
        },
        json: postData
    },function (error, response, body) {
        console.log('error:', error); // Print the error if one occurred
        console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
        console.log('body:', body); // Print the HTML for the Google homepage.
        res.json({
            'error': error,
            'statusCode': response && response.statusCode,
            'body': body
        })
    });
    

    【讨论】:

      【解决方案4】:

      the documentation。您只需在选项对象中包含 headers 属性的值:

      request.post(
          'https://server.com/index.php/rest/V1/integration/admin/token',
          {
              form: postData, /* Ensure this is a string of JSON! */
              headers:{ 
                  "Content-Type": "application/json"
              }
          },
          your_callback_function
      );
      

      【讨论】:

      • 仍在发送应用程序\/x-www-form-urlencoded
      猜你喜欢
      • 2011-07-24
      • 2012-04-07
      • 1970-01-01
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 2015-07-19
      相关资源
      最近更新 更多