尝试发布交易时出现422错误

时间:2016-05-09 17:32:12

标签: node.js square-connect unirest

我尝试使用带节点的uni-rest将事务发布到正方形。我相信我已经拥有了所有必需的参数,但是我从正方形中收到了422错误,在它所描述的描述中需要参考身体所需的参数'。将在下面发布一些代码示例和错误。

我的要求:

var json = {
          "idempotency_key": "74ae1696-b1e3-4328-af6d-f1e04d947a13",
          "shipping_address": {
            "address_line_1": "123 Main St",
            "locality": "San Francisco",
            "administrative_district_level_1": "CA",
            "postal_code": "94114",
            "country": "US"
          },
          "billing_address": {
            "address_line_1": "500 Electric Ave",
            "address_line_2": "Suite 600",
            "administrative_district_level_1": "NY",
            "locality": "New York",
            "postal_code": "10003",
            "country": "US"
          },
          "amount_money": {
            "amount": 5000,
            "currency": "USD"
          },
          "card_nonce": "CBASEA-NYZAdKADzd5FeF6kh0ko",  //sandbox nonce
          "reference_id": "some optional reference id",
          "note": "some optional note",
          "delay_capture": false
        }

//console.log(json)
return unirest.post('http://connect.squareup.com/v2/locations/9T8KRNF0XX6BH/transactions')
    .headers({'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization' : 'Bearer '+access_token, 'rejectUnauthorized': false})
    .type('json')
    .send(json)
    .end(function (json) {
      if(json.error){
        console.log(json.error);
      }
      console.log(json.body)
      res.json(json)
    });

}

我的错误:

{ Error: got 422 response
at /Users/.../node_modules/unirest/index.js:395:27
at process._tickCallback (internal/process/next_tick.js:98:9) status: 422 }
{ errors: [ { category: 'INVALID_REQUEST_ERROR',
   code: 'BAD_REQUEST',
   detail: 'body in body is required' } ] }

1 个答案:

答案 0 :(得分:1)

您通过HTTP而不是HTTPS发出请求。该API仅通过HTTPS提供。

我怀疑这种行为的原因是HTTP站点正在向您发送重定向到HTTPS站点。但是,除非您明确处理POST重定向,否则POST主体(以及可能还有其他必要的标头)会丢失,从而导致这种令人困惑的响应。