John
John

Reputation: 13719

Square API responds with empty JSON

Why am I receiving an empty response from the Square server?

I can't seem to get any sort of response once I change the location to one of the test locations. I'm using the correct sandbox token. Have I unintentionally omitted necessary information or triggered some sort of bug or security mechanism? I can not imagine an empty response being appropriate in any circumstance. Sensitive strings obfuscated for obvious reasons.

PHP

$url = 'https://connect.squareup.com/v2/locations/'.$location_id.'/transactions';
$curl_handle = curl_init($url);
$request_headers = array();
$request_headers[] = 'Content-Type: application/json';
$request_headers[] = 'Accept: application/json';
$request_headers[] = 'Authorization: Bearer '.$token;
$encodedData = json_encode($jsonData);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $encodedData);
$request_headers[] = 'Content-Length: '.strlen($encodedData);
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl_handle);

JSON used in cURL Request

{
"idempotency_key": "1",
"billing_address": {
"address_line_1": "123 Main Street",
"address_line_2": "",
"locality": "New York City",
"administrative_district_level_1": "NY",
"postal_code": "12345",
"country": "US"
},
"amount_money": {
"amount": 10,
"currency": "USD"
},
"delay_capture": false,
"buyer_email_address": "[email protected]",
"card_nonce": "122333444455555666666777777"
}

Using print_r(curl_getinfo($curl_handle)) I receive:

Array
(
 [certinfo] => Array()
 [connect_time] => 0.101375
 [content_type] => application/json
 [download_content_length] => 2
 [filetime] => -1
 [header_size] => 372
 [http_code] => 200
 [local_ip] => 66.198.240.51
 [local_port] => 44428
 [namelookup_time] => 0.009074
 [pretransfer_time] => 0.392582
 [primary_ip] => 74.122.190.85
 [primary_port] => 443
 [redirect_count] => 0
 [redirect_time] => 0
 [redirect_url] => 
 [request_size] => 734
 [size_download] => 2
 [size_upload] => 492
 [speed_download] => 3
 [speed_upload] => 979
 [ssl_verify_result] => 0
 [starttransfer_time] => 0.502108
 [total_time] => 0.502167
 [upload_content_length] => 492
 [url] => https://connect.squareup.com/v2/locations/__example__/transactions
)

Upvotes: 0

Views: 395

Answers (1)

tristansokol
tristansokol

Reputation: 4271

Is there are reason why you aren't using the PHP SDK? I think that would make this a little easier for you, keeping track of all these curl options looks kind of confusing.

You are sending a GET request to the transactions endpoint asking the API to list all your transactions. For GET requests, adding a body to your requests is not a good idea since it is ignored by nearly every API out there (like ours).

The API receives your GET request and returns to you your transactions which you probably haven't created any in your testing account likely looks like:

{}

This is the expected behavior. If you are trying to create a charge and not list your transactions, send a POST request instead, or just use the SDK to make things easier on you, see Charge

Upvotes: 1

Related Questions