I try to send request from post type to server but it not success, It return me BadRequest with status 400,
My code:
$products = array('1'=>array('amount'=>'1','product_id'=>'11250'));
$data = array('id' => '67', 'shipping' => '61', 'payment'=> '2','products'=> $products);
$cert = base64_encode('myapp@myapp.co.il:1234abcd');
$header = array(
"authorization: Basic ".$cert,
"Content-Type: application/json",
"cache-control: no-cache"
);
$url = "https://myapp.co.il/api/aaa";
$curl = curl_init($url);
//curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response,true);
var_dump($response);
?>
It return me so:
array (2) {["message"] => string (51) "Bad Request: Syntax error, distorted JSON" ["status"] => int (400)} But it returend me false
You set the Content-Type
request header to application/json
Then you use CURLOPT_POSTFIELDS
which is not for JSON, but a query string.
Change your headers to this:
$header = [
'authorization: Basic '.$cert,
'Content-Type: multipart/form-data',
'cache-control: no-cache'
];
And skip the http_build_query()
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.