node.js - 尝试发布交易时出现 422 错误

标签 node.js square-connect unirest

我正在尝试使用带有 Node 的 uni-rest 将交易发布到 square。我相信我拥有所有必需的参数,但是我收到了从 square 返回的 422 错误,在描述中它说“需要 body in body”。下面将发布一些代码示例和错误。

我的要求:

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 正文(也许还有其他必要的 header )将会丢失,从而导致这种令人困惑的响应。

关于node.js - 尝试发布交易时出现 422 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37121960/

相关文章:

Android Square 注册 API : ILLEGAL_LOCATION_ID

php - 将 unirest 转换为 curl

javascript - 错误 : Package exports for tslib do not define a valid '.' target

javascript - 使用 npm 打包 javascript 项目并将其划分为 Node 模块以创建依赖树

ios - Square iOS SDK如何抓取持卡人信息?

java - 在Netbean中使用unirest发送短信

java - 当 response.body 为空时,Unirest.delete() 会阻止任何进一步的 Unirest 方法

javascript - Mustache.js 递归

node.js - 使用 pdsh 远程运行 node.js pm2

ios - 如何使用 Square Point of Sale 应用程序的其他付款功能调用我的应用程序?