I am trying to json_decode the response I receive from a GET request to my server-side API but I am getting an empty string back. Would I be right in assuming that because the response contains all the header info that the JSON decoder cant cope? This is the full response I'm getting from my server:
HTTP/1.1 200 OK
Server: nginx/1.0.5
Date: Sun, 18 Mar 2012 19:44:43 GMT
Content-Type: application/json
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: Servlet/3.0; JBossAS-6
Content-Length: 97
{"pid":"162000798ab8481eaeb2b867e10f8849","uuid":"973b8722c75a4cacb9fd2316517587bb"}
Do I need to remove the headers in my servlet before I send the response to the client?
Yes, json_decode
must be passed just the JSON data to decode. Since you are using curl, you can simply configure the request to not return the headers to you with something like
curl_setopt($ch, CURLOPT_HEADER, false);
Update: if you need the headers for earlier processing then the above won't cut it. However, you can remove them easily at any point by taking advantage of the fact that there will be a double-newline "delimiter" between the header and body of the response. Using explode
like this will then isolate the body:
list(,$body) = explode("\n\n", $response, 2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With