0

I have be having a hard time sending a simple multipart form request using Guzzle. For some reason I always get a 404 response but I am make the same request using cURL I get 200 ok and the data is posted.

I can't seem to figure out what the issue is even after solving a similar issue.

Here's my cURL code:

$body = shell_exec('curl -H \'Content-Type: multipart/form-data\' -H \'Accept: application/json\' \
-F "photo=@ "'.$photo.'" \
-F "api_key="'.$apiKey.'" \
-F "id=14784" \
"'.$apiUrl.'" 2>&1 > /dev/null 2>&1 &');

The shell works fine from the command line but I need to convert it to make the request from a controller so I've added the form fields to Guzzle but not I get a 404.

Here's the Guzzle code:

 $guzzle = $client->request('POST', config('services.url'), [
                'multipart' => [
                    ['headers' => [
                            'Content-Type: multipart/form-data',
                            'Accept: application/json'
                        ],
                        'name'     => 'api_key',
                        'contents' => config('services.key'),
                    ],
                    [
                        'name'     => 'photo',
                        'contents' => $photo
                    ],
                    [
                        'name'     => 'id',
                        'contents' => "4084",
                    ],
                ]
            ]);

I've changed the url for the sake of posting but I'm struggling to find the cause so if anyone knows please give me a heads up!

** EDIT ** I've been able to get past the 404 but not I get a 500 Error, not sure if it is the form or the recipient server because I can make a POST request to the same endpoint successfully with Postman.

Still baffled.

user1704524
  • 427
  • 3
  • 15
  • 40
  • `'name' => 'photo=@',` ??? – miken32 Nov 23 '19 at 17:35
  • Yes I need to add that as a param, see the orignal cURL request – user1704524 Nov 23 '19 at 17:37
  • Also `Content-Type: multipart/form-data` in the curl code, and `application/x-www-form-urlencoded` in the Guzzle code? – miken32 Nov 23 '19 at 17:37
  • 1
    You should understand what the working code is doing before you try replicating it. Like what the significance of the `@` is. – miken32 Nov 23 '19 at 17:39
  • 1
    Does this answer your question? [Upload file using Guzzle 6 to API endpoint](https://stackoverflow.com/questions/38133244/upload-file-using-guzzle-6-to-api-endpoint) – miken32 Nov 23 '19 at 17:42
  • I'll have a look at the post, the '@' is part of the instruction for making a request with cURL that's as much as I know. – user1704524 Nov 23 '19 at 17:51

2 Answers2

2

there're 2 mistakes 1st you set base_uri and then you called it again

2nd mistakes

as you mentioned you're gonna using multipart

you should use multipart not form_params and you don't need json options on request part

header changed to this Content-Type' => multipart/form-data

therefore request should be look like this

$response = $client->post('/', [
    'multipart' => $params,
]);

reference can be seen here for multipart

Ghiffari Assamar
  • 650
  • 4
  • 14
1

When you set you base_uri you dont have to add it on the post. Most likely you will call and url similar to this http://httpbin.com/http://httpbin.com,.

$response = $client->post('/', ['json' => [
    'form_params' => [
        ....
    ],
]]);
mrhn
  • 17,961
  • 4
  • 27
  • 46