0

我正在尝试向 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"}
4

1 回答 1

1

您收到错误是因为您使用querystring.stringify了 ,它会生成一个 URL 编码的请求正文。您想JSON.stringify生成一个 JSON 编码的正文。IE:

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); });
于 2015-07-01T02:55:30.013 回答