0

I am trying to create a new Square user, but am having problems. I was able to use Unirest to GET a Square customer list at the https://connect.squareup.com/v2/customers endpoint, but haven't been able to successfully POST.

const unirest = require('unirest');
const access_token = 'sq-123...';

var createCustomer = 
unirest.post('https://connect.squareup.com/v2/customers')
  .headers({
    "Accept": "application/json",
    "Authorization": "Bearer " + access_token})
  .send({
    "email_address": "abc@example.com"
  })
  .end(function (createCustomer) {
    console.log(createCustomer);
});

The response returns

raw_body: 
'{"errors":[{
"category":"INVALID_REQUEST_ERROR",
"code":"BAD_REQUEST",
"detail":"invalid character \'e\' looking for beginning of value (line 1, character 1)"}]}' }

I also tried using .field instead of .send, which returned

raw_body: '{"errors":
[{"category":"INVALID_REQUEST_ERROR",
"code":"BAD_REQUEST",
"detail":"invalid character \'-\' in numeric literal (line 1, character 2)"}]}' }

Is it possible to create a customer using Unirest and Node? If so, where am I going wrong?

KVNA
  • 847
  • 1
  • 11
  • 24
  • Try sending `Content-Type: application/json` header to let the server know that data is of type json. You can use `type` of unirest also, e.g. `unireset.post().headers().type('json').send..` – Mukesh Sharma Apr 18 '17 at 03:23
  • OK, that worked using the ```.send``` command. Thank you! – KVNA Apr 18 '17 at 18:27

1 Answers1

0

The following code will POST

const unirest = require('unirest');
const access_token = 'sq-123...';

var createCustomer = 
unirest.post('https://connect.squareup.com/v2/customers')
  .headers({
    "Content-Type": "application/json",
    "Authorization": "Bearer " + access_token})
  .send({
    "email_address": "abc@example.com"
  })
  .end(function (createCustomer) {
    console.log(createCustomer);
});
KVNA
  • 847
  • 1
  • 11
  • 24