简体   繁体   English

request.post继续覆盖内容类型作为content-type:application / x-www-form-urlencoded当指定表单数据时

[英]request.post continues to override content-type as content-type: application/x-www-form-urlencoded when form-data is being specified

We have a client api call that requires a post be sent as form-data. 我们有一个客户端api调用,需要将一个帖子作为表单数据发送。 When we run the call through Chrome's Postman extension it runs successfully when we specify form-data, and returns an error if we specify x-www-form-urlencoded. 当我们通过Chrome的Postman扩展程序运行调用时,它在我们指定表单数据时成功运行,如果我们指定x-www-form-urlencoded则返回错误。 This is expected. 这是预料之中的。

However when trying to run in node.js using the "request" npm package to handle the post we continue to receive an error message from the api which unfortunately does not give us specifics as to what is wrong. 但是当尝试使用“request”npm包在node.js中运行来处理帖子时,我们继续从api收到一条错误消息,但遗憾的是,这并未向我们提供有关错误的具体信息。 But we do see that the request header object looks like this when it goes out: 但我们确实看到请求标头对象在出现时看起来像这样:

_header: 'POST /api/client/coupon/add HTTP/1.1\\r\\nAuthorization: Basic [auth string redacted]\\r\\nhost: beta1.client.com\\r\\ncontent-type: application/x-www-form-urlencoded\\r\\ncontent-length: 172\\r\\nConnection: keep-alive\\r\\n\\r\\n', _header:'POST / api / client / coupon / add HTTP / 1.1 \\ r \\ nAuthorization:Basic [auth string redacted] \\ r \\ nhost:beta1.client.com \\ r \\ ncontent-type:application / x-www-form -urlencoded \\ r \\ ncontent-length:172 \\ r \\ n \\ nConnection:keep-alive \\ r \\ n \\ r \\ n',

Our Node.js code looks like: 我们的Node.js代码如下:

  //Create the coupon
  var coupon = { code: "abcde1234"),
    discount: "33",
    type: "percent"
  }

  var request = require('request');
  request.post(
    {
      url: "https://beta1.client.com/api/coupon/add",
      headers: {
        "authorization": auth,
        "content-disposition": "form-data; name='data'"
      },
      form: coupon
    },

    function (error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log(body)
      }
    }
  );

What I am wondering is why the content-type header continues to read "application/x-www-form-urlencoded" when we have provided a content-disposition of 'form-data'. 我想知道的是当我们提供'form-data'的内容处理时,内容类型标题继续读取“application / x-www-form-urlencoded”的原因。 To me it seems like if I could remove the content-type header attribute it should work - but how to do that? 对我而言,似乎我可以删除它应该工作的内容类型头属性 - 但是如何做到这一点?

Any insights would be appreciated. 任何见解将不胜感激。

The reason why it is updating the Content-Type header to application/x-www-form-urlencoded is because you are using form in your POST request. 它将Content-Type标头更新为application/x-www-form-urlencoded的原因是因为您在POST请求中使用form

request.post({
  url: "https://beta1.client.com/api/coupon/add",
  headers: {
    "authorization": auth,
    "content-disposition": "form-data; name='data'"
  },
  form: coupon
}, function (error, response, body) {
  ...
});

In order to handle this, you need to add the content as body as provided below. 为了处理这个问题,您需要将内容添加为body ,如下所示。

request.post({
  url: "https://beta1.client.com/api/coupon/add",
  headers: {
    "authorization": auth,
    "content-disposition": "form-data; name='data'"
  },
  body: coupon
}, function (error, response, body) {
  ...
});

In the end we went with the form-data package found here: https://www.npmjs.com/package/form-data 最后,我们使用了这里找到的表单数据包: https//www.npmjs.com/package/form-data

Our code now looks like this: 我们的代码现在看起来像这样:

    //create coupon json and stringify it
    var coupon = {
      coupon: {
        code: couponCode,
        discount: discountPercentage,
        type: 'percent',
        product: productId,
        times: 1,
        expires: couponExpiresDt
      }
    };
    var couponString = JSON.stringify(coupon);

    //create form-data object to be posted to client api
    var FormData = require('form-data');
    var couponForm = new FormData();
    couponForm.append('data', couponString);

    //create submission options for the post
    var submitOptions = {
      hostname:config.client_api_host,
      path:'/api/2/coupon/add',
      auth:auth,
      protocol:'https:'
    };

    //submit the coupon/add post request
    couponForm.submit(submitOptions, function(err, res) {
      res.resume();

      if (err) {
        callback(err);
      } else if (res.statusCode != 200) {
        callback(new Error('API createDiscount post response error:', res.statusCode));
      } else {
        logger.log('info', "coupon code " + couponCode +  " has apparently been created");
        callback(null, {coupon_code: couponCode, expires: couponExpiresDt});
      }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用“Content-Type”:“application/x-www-form-urlencoded”从 axios 发送帖子请求会给出 401 Unauthorized 响应 - Sending post request from axios with "Content-Type" : "application/x-www-form-urlencoded" gives an 401 Unauthorized response node.js正文解析器对Content-Type:x-www-form-urlencoded和Form-data JSON的错误解释 - node.js body-parser bad interpretation of Content-Type:x-www-form-urlencoded and Form-data JSON 如何将Content-Type:appication / x-www-form-urlencoded更改为application / json? - How to change Content-Type: appication/x-www-form-urlencoded to application/json? 使用 apollo-datasource-rest 库将 Content-Type 标头设置为 application/x-www-form-urlencoded - Setting Content-Type header to application/x-www-form-urlencoded using apollo-datasource-rest library 如何使用“内容类型”:“ application / x-www-form-urlencoded”发出发布请求? - How to make a Post request with “content type”: “application/x-www-form-urlencoded”? 如何在 node.js 中发布内容类型 ='application/x-www-form-urlencoded' 的数据 - how to post data in node.js with content type ='application/x-www-form-urlencoded' 如何使发布请求具有bas ic auth和内容类型x-www-form-urlencoded - how to make post request baswith ic auth and content type x-www-form-urlencoded 通过 AJAX 作为 post 发送表单 - 这是更好的内容类型 multipart/form-data 或 application/json - sending form via AJAX as post - which is better content-type multipart/form-data or application/json 在请求中解码 JSON object,其中内容类型为“application/x-www-form-urlencoded” - Decode JSON object in req where content type 'application/x-www-form-urlencoded' axios 发布请求正在发送请求 header 的 Content-Type: multipart/form-data 导致未定义的 req.body - axios post request is sending a request header of Content-Type: multipart/form-data resulting in undefined req.body
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM