error 406 with put request the laravel guzzle

Viewed 173

I need help with a put request with the laravel 8 guzzle. I'm uploading a zip file. It is returning error 406.

With postman it's working fine. Returns 200. If anyone knows how to resolve help me with this please.

Thanks.

follow the code:

    $response = Http::withToken($token)->withHeaders([
        "Content-Type" => "application/json",
        "Accept" => "application/zip",
    ])->attach(
        'inputFile', $zip, 'zip'
    )->put($endPoint);

Log::alert($response); dd($response->status());

With postman it's working fine, returns 200. Here:

Postman

postman

1 Answers

You cannot send content type as application/json with attach. attach is used for Multi-Part Requests. It sets the body of the request to a multipart/form-data form.

Change to this

$response = Http::withToken($token)->attach(
        'inputFile', $zip, 'zip'
    )->put($endPoint);

or you can remove the whole header part. If the request wants you to send something else as json along with your zip, then use the original syntax of guzzle to add the part.

Related