1

I'm Trying to correctly use wp_remote_get in my wordpress plugin which uses our API. The API checks the request body for is_json, which returns 4 - aka JSON_ERROR_SYNTAX. Using cURL has the desired response. Here's my code:

$args = array( 'body' => array('api_key' => 123), 'timeout' => '5', 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'cookies' => array() ); $result = wp_remote_request('https://myapi.endpoint/api/1.0/request', $args); var_dump($result['body']) 

var_dump: string(13) ""api_key=123""

whereas the var_dump of my cURL request is string(15) "{"api_key=123"}"

Any ideas as to what I am doing incorrectly?

1
  • 2
    WP has no idea that the API is expecting json. You're stuffing in an array, which WP is probably going to be sending over as GET query parameters, not json. Commented Mar 3, 2014 at 19:21

1 Answer 1

1

As Marc B stated, wp isn't sending json. You need to set the content type in the headers, and also send it as json:

$body = array('api_key' => 123); $headers = array ( 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'Content-Length' => strlen(json_encode($body) ); $args = array( 'method' => 'POST', 'headers' => $headers, 'body' => json_encode($body) ); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.