I am trying to send a JSON payload to an endpoint using wp_remote_post()
Payload:
$body = [
'stock_data' => [
'post_title' => $title,
'post_content' => $content,
'post_date' => $postdate,
'post_category' => $categoryname
]];
But this is what shows up at the endpoint:
stock_data: [object Object]
however, the following works:
$body = [
'post_title' => $title,
'post_content' => $content,
'post_date' => $postdate,
'post_category' => $categoryname
];
and the payload is delivered correctly.
this is how I am sending the payload:
$body = wp_json_encode( $body );
$options = [
'body' => $body,
'headers' => [
'Content-Type' => 'application/json',
],
'timeout' => 60,
'redirection' => 5,
'blocking' => true,
'httpversion' => '1.0',
'sslverify' => false,
'data_format' => 'body',
];
wp_remote_post( $endpoint, $options );
What am I doing wrong?
CodePudding user response:
It looks to me like the body of your post is expected to be a single instance of that array with member names 'post_title'
, `'post_content', and the others).
In other words it looks like it wants you to deliver this, with no 'stock_data'
subarray.
{ "post_title":"title",
"post_content":"content",
"post_date":"timestamp",
"post_category":"category"
}
That's what you deliver in the instance where it works.
You should check the docs for the endpoint you use.
CodePudding user response:
Based on your comments, the correct PHP array would look like this
$body = array (
'stock_data' =>
array (
'post_title' => 'title',
'post_content' => 'content',
'post_date' => 'timestamp',
'post_category' => 'category',
),
)