Reputation: 458
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.
Upvotes: 0
Views: 737
Reputation: 661
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
Upvotes: 2
Reputation: 18926
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' => [
....
],
]]);
Upvotes: 1