AFNetworking failing with “Garbage at end” when using php’s cURL

If you don’t set CURLOPT_RETURNTRANSFER to true, cURL outputs the results of the CURL operation directly into the response, which is then sent back to AFNetworking. If the cURL operation outputs anything but valid JSON, this most likely will cause the JSON decoding to fail with:

[sourcecode language=”plain”]Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=0x753e5c0 {NSDebugDescription=Garbage at end.}[/sourcecode]

To turn on CURLOPT_RETURNTRANSFER, do:
[sourcecode language=”plain”]curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);[/sourcecode]

Hope this helps prevent some hair-pulling during midnight debugs.

Leave a Reply

Your email address will not be published. Required fields are marked *