Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

arthurhetem's avatar

Guzzle Post escapes url in json body

Hello, i'm trying to do a post http request to a API that requires me to send a callback url in the request json body, but when i send the post request, the url get its slashes escaped with backslashes:

$request = Http::ddWithCurl()->asJson()->acceptJson()->withOptions(
          ['debug' => true,]
        );

$response = $request->post('https://id.magalu.com/oauth/token', [
            'client_id' => $magalu->client_id,
            'client_secret' => $magalu->client_secret,
            'redirect_uri' => 'https://hagil.nortesulvirtual.com/api/integrations/magalu',
            'code' => $magalu->code,
            'grant_type' => 'authorization_code',
        ]);

this generates the following curl request:

curl -H 'Content-Length: 288' -H 'User-Agent: GuzzleHttp/7' -H 'Host: id.magalu.com' -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{"client_id":"HIDDEN","client_secret":"HIDDEN""redirect_uri":"https:\/\/hagil.nortesulvirtual.com\/api\/integrations\/magalu","code":"HIDDEN","grant_type":"authorization_code"}' -X 'POST' 'https://id.magalu.com/oauth/token'
0 likes
1 reply
s4muel's avatar
s4muel
Best Answer
Level 50

i dont think it is sent with the backslashes. use some http echo service and try it for yourself

$request = Http::asJson()->acceptJson();

$response = $request->post('https://echo.free.beeceptor.com', [
    'redirect_uri' => 'https://hagil.nortesulvirtual.com/api/integrations/magalu',
]);

dump($response->body());

i see you are using ddWithCurl() so i assume it is this package https://github.com/jigar-dhulla/laravel-http-to-curl (or similar).

the curl part from json is generated by this line: https://github.com/jigar-dhulla/laravel-http-to-curl/blob/main/src/CurlCommandLineGenerator.php#L25

which uses json_encode() without JSON_UNESCAPED_SLASHES option, so it is like calling

dump(json_encode(['redirect_uri' => 'https://hagil.nortesulvirtual.com/api/integrations/magalu']));
//"{"redirect_uri":"https:\/\/hagil.nortesulvirtual.com\/api\/integrations\/magalu"}"

//if they used the option
json_encode(['redirect_uri' => 'https://hagil.nortesulvirtual.com/api/integrations/magalu'], JSON_UNESCAPED_SLASHES);
//"{"redirect_uri":"https://hagil.nortesulvirtual.com/api/integrations/magalu"}"



but it should be no problem at all

Please or to participate in this conversation.