问题
I'm getting a "Value was not expected to be a string" error. I've validated the JSON request with a validator and I've ensured that all values are enclosed within double quotes. However when it tells me the length (in this example, 500) it is greater than the length of the JSON itself.
[category] => INVALID_REQUEST_ERROR
[code] => BAD_REQUEST
[detail] => Value was not expected to be a string (line 1, character 500)
Using:
{
"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": "someone@example.com",
"card_nonce": "C257wsh4fggd1OWEQLTwU0MIdnA"
}
This error message is too vague. How do I convert the unknown variable from a string to the unknown desired data type?
回答1:
The issue here is that you are trying to specify the amount
in amount_money
as a string instead of a number, same thing for delay_capture
. try
"amount": 10,
"delay_capture": false,
I agree, the error message is a little confusing, I'll see if I can get it improved.
回答2:
A secondary issue: if the data is already formatted as JSON ensure that the JSON is not being encoded a second time!
//$encodedData = json_encode($json);//double-encoded code issue here!
$encodedData = $json;
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $encodedData);
$request_headers[] = 'Content-Length: '.strlen($encodedData);
来源:https://stackoverflow.com/questions/46477085/square-value-was-not-expected-to-be-a-string