• 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 ]

    • This topic was modified 5 years, 4 months ago by linch1.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @linch1

    Try with below code.

    $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>';
    }

    Thanks
    Ahir

    • This reply was modified 5 years, 4 months ago by Ahir Hemant.
    Thread Starter linch1

    (@linch1)

    Thanks for the reply @hemant-ahir your code snippet correctly works, also i figured out that the problem was in changing the headers.
    I mean I don’t understand why if i try to change set for example
    $headers = array( 'asd' => 'asd' );
    or
    $headers = array( 'origin' => 'localhost' );
    the request sends an empty body.

    Edit: I’m wrong i was missing the
    'Content-Type' => 'application/json'

    • This reply was modified 5 years, 4 months ago by linch1.
    • This reply was modified 5 years, 4 months ago by linch1.
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘wp_remote_post sends empty body’ is closed to new replies.