[英]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.