palla451
1 month ago
242
7
General

Guzzle/Http Client Error method Patch

Posted 1 month ago by palla451

Hi all guys. I have this question:

i make a patch request http in postman

i set in Headers

'Content-type' application/json

Body raw:

{"id":0,"name":"string","descriptions":[{"message":"string","locale":"string"}],"type":"string","isTemplate":true,"logType":"string","logFormat":"string","knownExecs":[{"filename":"string","absolutePath":"string","hashCodes":[{"hashType":"string"}]}]}

if i set the call in my laravel project in my controller:


    public function requestPatchExectLists($url, $id)
    {
        $body = [
            'id'=> 0,
            'name'=> "string",
            'descriptions'=> [
                ['message'=> 'string', 'locale'=> 'string']
            ],
            'type'=> 'string',
            'isTemplate'=> true,
            'logType'=> "string",
            'logFormat'=> 'string',
            'knownExecs'=> [
                [
                    'filename'=> 'string',
                    'absolutePath'=> 'string',
                    'hashCodes'=> [
                    ['hashType'=> 'string']
                    ]
                ]
            ]
        ];

        try {
            $response = $this->client->request('PATCH',$url, [
                'form_params' => $body,
                'headers' => ['Content-Type'=>'application/json']
            ]);
        } catch (GuzzleException $error) {
            $response = $error->getResponse();
            // Get the info returned from the remote server.
            $response_info = $response->getBody()->getContents();
            return $response_info;
        }

        return $this->response_handler($response->getBody()->getContents());
    }

    public function response_handler($response)
    {
        if ($response) {
            return json_decode($response);
        }

        return [];
    }

i have this error :

"{\"error\":{\"code\":\"invalid_json\",\"message\":\"Invalid JSON\"}}"

But if i show json_encode($body) i have the same result of raw params in postman:

{"id":0,"name":"string","descriptions":[{"message":"string","locale":"string"}],"type":"string","isTemplate":true,"logType":"string","logFormat":"string","knownExecs":[{"filename":"string","absolutePath":"string","hashCodes":[{"hashType":"string"}]}]}

Where is my error please......

Please sign in or create an account to participate in this conversation.