无法将POST请求标头设置为JSON

时间:2019-02-06 09:44:09

标签: json wordpress api header content-type

我正在使用wordpress wp_remote_post尝试将POST请求有效负载设置为JSON的API。

API之所以拒绝该请求,是因为它以“ Content-Type:application / x-www-form-urlencoded”(而不是“ Content-Type:application / json”)接收请求

我尝试将内容类型设置为json并在请求正文上运行json_encode,但无济于事。

我不确定还有什么尝试。有人可以在这里为我提供指针吗?

非常感谢

function call_api_test( $endpoint = '', $args = '' ) {
    $api_url = get_field( 'api_url', 'option' );
    $request = array(
            'grant_type'=> 'client_credentials',
    'client_id'=> get_field( 'api_client_id', 'option' ),
    'client_secret' => get_field( 'api_client_secret', 'option' )
    );
    $token_response = wp_remote_post( $api_url . '/oauth/token', array(
    'headers' => array('Content-Type' => 'application/json; charset=utf8'),
    'body' => json_encode($request),
    'data_format' => 'body',
    'sslverify' => false
));
$token_object = json_decode(wp_remote_retrieve_body($token_response));
$request_url = $api_url . '/' . $endpoint . '?access_token=' . 
$token_object->access_token . '&' . $args;
$api_response = wp_remote_get( $request_url, 'sslverify=0' );
$response = json_decode( wp_remote_retrieve_body( $api_response ) );
return $response;

}

我希望请求标头的内容类型为application / json,但是它们是application / x-www-form-urlencoded

0 个答案:

没有答案
相关问题