Sending JSON Payload using Request::request_multiple()
Question
I have an external api which takes json body as payload. I am trying to send multiple parallel requests. So I am not using wp_remote_post()
The wp_remote_post() version of my code works perfectly.
Sending JSON payload using wp_remote_post()
This works!
$response = wp_remote_post($url, [
'body' => json_encode($registrants), // requried keys [firstName, lastName, email]
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
'Content-Type' => 'application/json; charset=utf-8'
],
'data_format' => 'body',
'timeout' => 10,
]);
return json_decode(wp_remote_retrieve_body($response));
Now I am trying to do the same with Request::request_multiple()
But data isn’t sending as json body.
Sending JSON payload using Request::request_multiple()
Does not work!
$requests[] = [
'url' => $url,
'type' => 'POST',
'body' => json_encode($registrant), // requried keys [firstName, lastName, email]
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
'Content-Type' => 'application/json; charset=utf-8'
],
'data_format' => 'body',
'timeout' => 30,
];
$options = [
'data_format' => 'body'
];
$resp = Requests::request_multiple($requests, $options);
foreach($resp as $response){
var_dump($response);
$responses[] = json_decode($response->body);
}
The error I am getting from API is very specific and throws when it doesn’t get JSON body payload.
0
http-api, requests-class
3 years
2020-06-13T11:10:29-05:00
2020-06-13T11:10:29-05:00 0 Answers
99 views
0
Leave an answer