尝试 post 事务时出现 422 错误

422 error when trying to post transaction

我正在尝试 Post 使用带有节点的 uni-rest 对交易进行平方。我相信我拥有所有必需的参数,但是我从 Square 收到 422 错误,在描述中它说 'body in body required'。将 post 下面的一些代码示例和错误。

我的要求:

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' } ] }

您正在通过 HTTP 而不是 HTTPS 发出请求。 API 仅通过 HTTPS 提供服务。

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