2

Working with the remote API requests with WordPress. I have used wp_remote_post() function to request the HTTP data but I am unable to update the headers Content-type I am using the following code

 wp_remote_post( $url, array(
    'method' => 'POST',
    'timeout' => 45,
    'redirection' => 5,
    'httpversion' => '1.0',
    'blocking' => true,
    'headers' => array("Content-type" => "application/x-www-form-urlencoded;charset=UTF-8"),
    'body' => array( 'username' => 'bob', 'password' => '1234xyz' ),
    'cookies' => array()
    )
);

But when I print the response content-type is still text/html but the server require urlencoded content-type. How i can update the headers content type?

  • 3
    Default header is already application/x-www-form-urlencoded. What you see on printing response is response header which is control by server. The one you are setting is request header. So where is the issue ? – Sumit Mar 29 '16 at 17:21
4

So, you set the headers of a request using wp_remote_post() and you expect that headers be used in the response. That is what I understand from you:

When I print the response content-type is still "text/html"

I think that you are missunderstanding the HTTP headers. Using wp_remote_post() you make a request and you can set the headers of the request to whatever you need but obviously you can not control the headers of the response.

The headers of the response depends absolutely in the remote server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.