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

api - Post body not working with wp_remote_post()

programmeradmin8浏览0评论

I'm trying to post with with the following code

    $userTokenApi = '';

    $args = array(
        'headers' => array(
            'Content-Type' => 'application/json',
            'SiteId' => '6387',
            'Api-Key' => '7bba39594b4d460293abdfd64c8eea48'
        ),
        'body' => array(
            'Username' => 'myusername',
            'Password' => 'mypassword'
        )
    );

    $request = wp_remote_post($userTokenApi, $args);
    $responseCode = wp_remote_retrieve_response_code( $request );
    $body = wp_remote_retrieve_body($request);

    if ( is_wp_error( $request ) ) {
        return false; // Bail Early
    }

    $pretty = json_decode( $body ); ?>

But the response I'm getting back from the API is

Error:
Code: "MissingRequiredFields"
Message: "The following parameters are required: Username, Password"

The standard HTTP request for the same action (with PHP) is seen here and using postman I'm able to post and receive my response fine with PHP - HTTP Request2, PHP - cURL, and any other type of code.

I'm not sure what I'm missing here or what I don't understand in the documentation

Any help would be amazing. Live issue can be seen here - error's in console.

I'm trying to post with with the following code

    $userTokenApi = 'https://api.mindbodyonline/public/v6/usertoken/issue';

    $args = array(
        'headers' => array(
            'Content-Type' => 'application/json',
            'SiteId' => '6387',
            'Api-Key' => '7bba39594b4d460293abdfd64c8eea48'
        ),
        'body' => array(
            'Username' => 'myusername',
            'Password' => 'mypassword'
        )
    );

    $request = wp_remote_post($userTokenApi, $args);
    $responseCode = wp_remote_retrieve_response_code( $request );
    $body = wp_remote_retrieve_body($request);

    if ( is_wp_error( $request ) ) {
        return false; // Bail Early
    }

    $pretty = json_decode( $body ); ?>

But the response I'm getting back from the API is

Error:
Code: "MissingRequiredFields"
Message: "The following parameters are required: Username, Password"

The standard HTTP request for the same action (with PHP) is seen here https://developers.mindbodyonline/PublicDocumentation/V6#user-tokens and using postman I'm able to post and receive my response fine with PHP - HTTP Request2, PHP - cURL, and any other type of code.

I'm not sure what I'm missing here or what I don't understand in the documentation

Any help would be amazing. Live issue can be seen here - error's in console.

Share Improve this question asked Aug 9, 2020 at 12:02 Andrew-SepicAndrew-Sepic 1671 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

You're setting the Content-Type header to application/json and wp_remote_post() doesn't intelligently JSON-encode the request data (the body array), so you should manually do it. So for example:

'body' => json_encode( array(
    'Username' => 'myusername',
    'Password' => 'mypassword'
) )
发布评论

评论列表(0)

  1. 暂无评论