Reputation: 2522
I am attempting to send a json string in a curl post. My vendor is telling me the request is being truncated. I am at a loss.
my code:
$url = "https://someServer/json.pl/vendor_account_numbers/list";
$key = "XXX";
$secret = "XXX";
$hash = hash_hmac('SHA1',$url,$secret,$raw_output = false);
$array = array(
"json" => array(
"criteria" => array(
"vendor_id" => array(
"operator" => "=","data" => "2"
)
)
)
);
$post = json_encode($array);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'HMAC:'.$hash,
'KEY:'.$key,
'Content-Type: application/json charset=utf-8',
'Content-Length: '.strlen($post),
));
echo strlen($post);
curl_setopt($ch,CURLOPT_POSTFIELDS,"json=".$post);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);
print_r(curl_getinfo($ch));
print_r($result);
$res = json_decode($result);
exit();
The vendor says they are seeing:
json:{"criteria":{"vendor_id":{"operator":"=","data":"
Upvotes: 1
Views: 1131
Reputation: 2522
I was getting the strlen of the post, but in my curl i was adding "json=", thus making the content-length incorrect.
$post = json_encode($array);
$length = strlen($post)+5;
I did 5 because of the json bit in my postfields:
curl_setopt($ch,CURLOPT_POSTFIELDS,"json=".$post);
Upvotes: 2
Reputation: 598
Try changing this:
'Content-Type: application/json charset=utf-8',
to this:
'Content-Type: application/json; charset=utf-8',
Note the semi-colon after the mime type.
If the system doesn't have a content-length requirement (many browsers and some web services do), then set that to zero.
Upvotes: 0