Search code examples
node.jssquare-connectunirest

422 error when trying to post transaction


Im attempting to Post a transaction to square using uni-rest with node. I believe I have all of the required parameters, how ever I'm receiving a 422 error back from square, in the descriptions it says 'body in body required'. Will post some code samples and error below.

My request:

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

}

My error:

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

Solution

  • You are making the request over HTTP instead of HTTPS. The API is served over HTTPS only.

    I suspect the reason for this behavior is that the HTTP site is sending you a redirect to the HTTPS site. However, unless you are explicitly handling POST redirects, the POST body (and perhaps other necessary headers as well) is getting lost, thus causing this confusing response.