无法从Squareup API获得正确答案

时间:2015-05-08 09:17:13

标签: node.js request square-connect

我试图向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"}

1 个答案:

答案 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); });