簡體   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

我們有一個客戶端api調用,需要將一個帖子作為表單數據發送。 當我們通過Chrome的Postman擴展程序運行調用時,它在我們指定表單數據時成功運行,如果我們指定x-www-form-urlencoded則返回錯誤。 這是預料之中的。

但是當嘗試使用“request”npm包在node.js中運行來處理帖子時,我們繼續從api收到一條錯誤消息,但遺憾的是,這並未向我們提供有關錯誤的具體信息。 但我們確實看到請求標頭對象在出現時看起來像這樣:

_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',

我們的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)
      }
    }
  );

我想知道的是當我們提供'form-data'的內容處理時,內容類型標題繼續讀取“application / x-www-form-urlencoded”的原因。 對我而言,似乎我可以刪除它應該工作的內容類型頭屬性 - 但是如何做到這一點?

任何見解將不勝感激。

它將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) {
  ...
});

為了處理這個問題,您需要將內容添加為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) {
  ...
});

最后,我們使用了這里找到的表單數據包: https//www.npmjs.com/package/form-data

我們的代碼現在看起來像這樣:

    //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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM