cloudflare-php icon indicating copy to clipboard operation
cloudflare-php copied to clipboard

Guzzle plaintext response error

Open dumityty opened this issue 4 years ago • 2 comments

This is a PR to fix the issue raised in https://github.com/cloudflare/cloudflare-php/issues/152 which I've also encountered after applying the patch from https://github.com/cloudflare/cloudflare-php/pull/139 to be able to use the WorkersKV endpoints.

The fix is quite basic and there might be a better of doing this, but from what I can see whenever there's either success or error responses they are always in JSON format, except for the one random endpoint to 'Read key-value pair ' (https://api.cloudflare.com/#workers-kv-namespace-read-key-value-pair) which returns the value of the key in plain text. So to get around that I just checked if the first character of the response is a { or not, and if it's not then skip all the json code below otherwise it fails with a 'json syntax' error since it's a plain text.

I ran all the tests and they passed, however I didn't write a new one as the change is to the Guzzle adapter which already has all the tests. I guess I could try writing one to cover the 'plain text' response from the API.

dumityty avatar Feb 26 '21 09:02 dumityty

I'll need to investigate this, as json_decode on the responses from that sample endpoint (e.g. "Some Value", note the double-quotes) are correctly decoded from a bare JSON string into a PHP string. That is to say, the responses for that endpoint are indeed valid JSON, albeit not wrapped in an object as is typically the case.

deuill avatar Mar 08 '21 16:03 deuill

I've opened a perhaps more concrete fix to the issue you're seeing here under #164.

deuill avatar Mar 10 '21 11:03 deuill