我试图向Square发送帖子消息以创建项目
var postData = {
"name": "Milkshake",
"variations": [
{
"name": "Small",
"pricing_type": "FIXED_PRICING",
"price_money": {
"currency_code": "USD",
"amount": 400
}
}
]}
request.post({
uri:"https://connect.squareup.com/v1/me/items",
headers:{'Authorization': 'Bearer ' + access_token,
'Accept': 'application/json',
'Content-Type': 'application/json'},
body: querystring.stringify(postData)
},function(err,res,body){
console.log(res.statusCode);
console.log(body); });
但是我从Square
收到此消息{"type":"bad_request","message":"invalid json"}
答案 0 :(得分:1)
您因为使用querystring.stringify
而产生错误,因为它生成了一个URL编码的请求正文。您希望JSON.stringify
生成JSON编码的正文。即:
request.post({
uri:"https://connect.squareup.com/v1/me/items",
headers:{'Authorization': 'Bearer ' + access_token,
'Accept': 'application/json',
'Content-Type': 'application/json'},
body: JSON.stringify(postData)
},function(err,res,body){
console.log(res.statusCode);
console.log(body); });