0

I am having an issue with an app created in Lumen and Guzzlehttp requests.

Looks like I cannot pass options like JSON_UNESCAPED_SLASHES whenever I am doing a request:

$response = (new Client())->request($this->typeRequest, $endpoint, $options);

This is hitting my server with escaped slashes ("one\/two") and causing some troubles.

Everything seems to be related with the vendor/guzzlehttp/guzzle/src/Client.php into applyOptions() function which is using jsonEncode and not giving the option to pass anything:

$options['body'] = Utils::jsonEncode($options['json']);

This can be easily fixed just putting the option into jsonEncode:

$options['body'] = Utils::jsonEncode($options['json'], JSON_UNESCAPED_SLASHES);

The issue here is in case I am updating something with composer then will be override.

How can I resolve an issue like this?

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Leizar999
  • 1
  • 3
  • Sounds like the issue is on the server side, not the client side. Forward slashes do not need to be escaped. Is it perhaps parsing the JSON string with regex? – Alex Howansky Dec 27 '21 at 21:33
  • Exactly, by default slashes are scaped like @ÁlvaroGonzález said, the issue here is that I cannot hit the server with escaped slashes otherwise will lead to a problem, and seems that I cannot make it work passing any option that will be taken by json_encode function that is used – Leizar999 Dec 28 '21 at 10:12
  • Yes sorry, this is a TYPO, what I meant is that is hitting the server with escaped backslashes, still trying to figure it out a better solution here, but looks like I end always at the same spot. – Leizar999 Dec 28 '21 at 17:29
  • I've fixed the typo myself since it was altering completely the meaning of the question. Remember that Stack Overflow allows (and encourages) to edit your own questions in order to fix errors or provide additional information. – Álvaro González Dec 30 '21 at 10:42

1 Answers1

0

You can just pass body directly:
['body'=>json_encode($data, JSON_UNESCAPED_SLASHES)].

use GuzzleHttp\Psr7\Request;
//...
$request = new Request('POST', $endpoint, ['content-type' => 'application/json'], json_encode($data, JSON_UNESCAPED_SLASHES));
$response = (new Client())->send($request);
Anton
  • 728
  • 3
  • 8
  • Seems that this is not allowed anymore on Guzzle 7: "Passing in the "body" request option as an array to send a request is not supported. Please use the "form_params" request option to send a application/x-www-form-urlencoded request, or the "multipart" request option to send a multipart/form-data request." – Leizar999 Dec 28 '21 at 10:10
  • @Leizar999 than you need just compose it manually, please, see update – Anton Dec 28 '21 at 10:28
  • the Idea looks good, I am following this pattern, but I see that the function send() is specting two params: `public function send(RequestInterface $request, array $options = []): ResponseInterface` if I am passing the $options I am ending in the same spot with the escaped backslashes – Leizar999 Dec 28 '21 at 17:24
  • You don't need pass `json` options key (payload is already in request). – Anton Dec 29 '21 at 09:41
  • It worked! there was an issue creating the client, I had to pass there some options like `base_uri` since it was throwing a curl #3 error. I will update the post in case someone is looking for something like this. – Leizar999 Dec 29 '21 at 15:31