最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp remote request - wp_remote_post sends empty body

programmeradmin0浏览0评论

I made an api with nodejs and i'm trying to make some calls to it throught wordpress, all the requests works correctly but when i add a request body it is sent empty, i tried to search what was the problem but i found nothing that worked.

I mean if i send this request

$body = [
    'name'  => 'Pixelbart',
    'email' => '[email protected]',
    "password" => "Pass#your!word"
];
 
$body = json_encode( $body, TRUE );

echo $body; // here the body is correctly populated

$res = wp_remote_post("http://localhost:3000/users",
  array( 
    'headers' => array( 
      'Origin' => "http://localhost"
    ),
    'body' => $body,
    'method'      => 'POST',
    'data_format' => 'body'
  )
);

on the api side if i try to read the request body it is an empty JSON. As i thought that was an api problem i tried to send the request with other tools but if i use postman for send the reqeust in the same way it correctly works. Am i missing something? I tried also to remove all the plugins and all the custom imports for have a clear enviroment but the same error happens

[ Also ajax requesto from javascript fails if sent from the active wordpress theme ]

I made an api with nodejs and i'm trying to make some calls to it throught wordpress, all the requests works correctly but when i add a request body it is sent empty, i tried to search what was the problem but i found nothing that worked.

I mean if i send this request

$body = [
    'name'  => 'Pixelbart',
    'email' => '[email protected]',
    "password" => "Pass#your!word"
];
 
$body = json_encode( $body, TRUE );

echo $body; // here the body is correctly populated

$res = wp_remote_post("http://localhost:3000/users",
  array( 
    'headers' => array( 
      'Origin' => "http://localhost"
    ),
    'body' => $body,
    'method'      => 'POST',
    'data_format' => 'body'
  )
);

on the api side if i try to read the request body it is an empty JSON. As i thought that was an api problem i tried to send the request with other tools but if i use postman for send the reqeust in the same way it correctly works. Am i missing something? I tried also to remove all the plugins and all the custom imports for have a clear enviroment but the same error happens

[ Also ajax requesto from javascript fails if sent from the active wordpress theme ]

Share Improve this question edited Jan 30, 2021 at 18:24 Mat.C asked Jan 30, 2021 at 17:57 Mat.CMat.C 1114 bronze badges 2
  • Post data should be sent in the body as an array. - developer.wordpress/reference/functions/wp_remote_post – Q Studio Commented Jan 30, 2021 at 18:13
  • @QStudio actually i saw that in this way sould work also, but idk. Anyway i've already tried to send them as array but didn't work. Also ajax requesto from javascript fails if sent from the active wordpress theme – Mat.C Commented Jan 30, 2021 at 18:17
Add a comment  | 

1 Answer 1

Reset to default 1

The following code worked source. The 'Content-Type' => 'application/json' header was missing causing the problem

$url = 'myAPIURL HERE';
$username = 'apiuser';
$password = 'passwd';
$headers = array( 
     'Authorization' => 'Basic ' . base64_encode( "$username:$password" ), 
     'Content-Type' => 'application/json' 
);
$fields = array(
    'body' => json_encode(
        array(
         'email'     => '[email protected]',
         'name'      => 'Pixelbart',
         'password' => 'Pass#your!word'
        )
    ),
    'headers'     => $headers,
    'method'      => 'POST',
    'data_format' => 'body'
);

$response = wp_remote_post($url,$fields);

if ( is_wp_error( $response ) ) {
     $error_message = $response->get_error_message();
     echo "Something went wrong: $error_message";
} else {
     echo 'Response:<pre>';
     print_r( $response );
     echo '</pre>';
}
发布评论

评论列表(0)

  1. 暂无评论