I have problem about post using guzzle with multipart/form-data. i using laravel 8 and guzzle version ^7.3.
this is my code
but i get this result
Help me, please
Thanks
Each entry of the multi-part form should have a contents property. You have to change content to contents.
$response = $client->request('POST', 'http://httpbin.org/post', [
'multipart' => [
[
'name' => 'field_name',
'contents' => 'abc'
],
[
'name' => 'other_file',
'contents' => 'hello',
'filename' => 'filename.txt',
'headers' => [
'X-Foo' => 'this is an extra header to include'
]
]
]
]);
Checkout the docs: Guzzle Post Form Requests
Related
I am trying to create an order in paypal with laravel and guzzle and it throws me this error:
GuzzleHttp\Exception\ClientException Client error: POST https://api-m.sandbox.paypal.com/v2/checkout/orders resulted in a
400 Bad Request response:
{"name":"INVALID_REQUEST","message":"Request is not well-formed,
syntactically incorrect, or violates schema.","debug_id (truncated...)
my controller code:
$accessToken = $this->getAccessToken(); $client = new Client(['base_uri' => 'https://api-m.sandbox.paypal.com/v2/checkout/']);
$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
];
$params = [
'intent' => 'CAPTURE',
'purchase_units' => [
'amount' => [
'currency_code' => 'USD',
'value' => '100.00'
]
]
];
//dd($params);
$response = $client->request('POST', 'orders', [
'headers' => $headers,
'form_params' => $params
]);
Your $params object is invalid. It should be a list of purchase_unit items, not an associative array.
$params = [
'intent' => 'CAPTURE',
'purchase_units' => [
[
'amount' => [
'currency_code' => 'USD',
'value' => '100.00'
]
]
]
];
https://developer.paypal.com/api/orders/v2/#orders-create-request-body
'form_params' => $params
This is used to send an application/x-www-form-urlencoded POST request, which the PayPal API does not use.
You should be posting a plain string, JSON encoded. In place of form_params try passing the json key to guzzle in the request, or read its documentation on how to send json
edit: not sure whether that change makes a difference, but the other answer is correct that purchase_units needs to be an indexed array of purchase_unit objects -- likely only one of them.
i'm integrating this login api on laravel:
endpoint: http://127.0.0.1:8888/api/v1/users/login/
parameter:
{"data":{
"email":"admin#mail.com",
"password":"admin#123",
"user_type":"1",
"encrypted":false
},"encoded_data":"yes"}
controller:
i'm posting data from form:
$request->validate([
'username' => 'required',
'password' => 'required',
'user_type' => 'required'
]);
$ds = $request->all();
$url = $this->base_api_url . 'api/v1/users/login/';
$apiRequest = [
'data' => [
'email' => $ds['username'],
'password' => $ds['password'],
'user_type' => $ds['admin'],
'encrypted' => false
],
'encoded_data' => 'yes',
];
$apiResponse = Http::acceptJson()->post($url, $apiRequest);
dd($apiResponse);
its return:
{"error":1,"success":false,"message":"Undefined index: data"}
this is the parameter i'm sending in $apiRequest:
array:2 [▼
"data" => array:4 [▼
"email" => "admin#mail.com"
"password" => "admin#123"
"user_type" => "1"
"encrypted" => false
]
"encoded_data" => "yes"
]
api working fine on postman:
I think you are requesting json format data so you need to get with by using this$request->json()->all() by using this you can access the key value.
Laravel Http::post sends a normal post request (form data). You should also send the Content-type: application/json header so the Laravel API understands it.
Try using the following:
$apiResponse = Http::accept('application/json')->post($url, $apiRequest);
// or
$apiResponse = Http::acceptJson()->post($url, $apiRequest);
I have this code here. Basically i want to make a post request to my weebhook but it is not making a successful request and it is returning 400 as a status code , what i am missing
Route::get('/testime',function (){
$response = Http::post(env('SLACK_WEBHOOK'),[
'content' => "Testing testing",
'embeds' => [
[
'title' => "Test ",
'description' => "URRA",
'color' => '7506394',
]
],
]);
return $response->status();
});
You are receiving a 400 error, which is Slack telling you, your data is missing or malformed. To debug you can do the following. Which will most likely state what is wrong.
dd($response->body);
Your structure seems like nothing Slack supports, which most likely will be the error message. See examples here, not certain this will even work.
$response = Http::post(env('SLACK_WEBHOOK'), [
'channel' => 'Your channel id',
'text' => 'Testing testing',
'blocks' => [
[
'type' => 'header',
'text' => [
'type' => 'plain_text',
'text' => 'URRA',
'color' => '#36a64f',
],
],
],
]);
I'm trying to upload a csv file to a cloud storage using Laravel and Guzzle. The file does get successfully uploaded, but the problem is that for some reason the headers are written to the file along with the original content after the upload. That also happens when I upload the file through Postman. How can this be prevented and what is the correct way of sending such request ? Here's the snippet and the uploaded file content :
$res = $client->request('POST', $uri, [
'headers' => [
'Authorization' => 'Bearer '. $egnyteToken,
'Content-Type' => 'text/csv'
],
'multipart' => [
[
'name' => $file->getClientOriginalName(),
'contents' => File::get($file),
'filename' => $file->getClientOriginalName(),
]
]
]);
Seems that the server doesn't require multipart body, so because of this you see all content that you sent in the uploaded file.
Just use body and not multipart.
$res = $client->request('POST', $uri, [
'headers' => [
// ...
],
'body' => File::get($file),
]);
How make post request with GuzzleHttp( version 6.0 ). I am trying do the following and getting error
i'm get the image value
Illuminate\Http\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 1.53mb.jpg
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
[size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 1607671
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[pathName:SplFileInfo:private] => C:\wamp\tmp\php32BB.tmp
[fileName:SplFileInfo:private] => php32BB.tmp
)
here i'm using guzzlehttp using to upload the image
$response = $this->client->request('POST', url('/update_profile'), [
'multipart' => [
[
'name' => 'foo',
'contents' => fopen('C:\wamp\tmp\php32BB.tmp', 'r'),//this path is image save temperary path
]
]
]);
Now i get the error
fopen(C:\wamp\tmp\php17BC.tmp): failed to open stream: No such file or directory.
How to use the contents in multipart
i solved this issue
$image_path = $post_array['image']->getPathname();
$image_mime = $post_array['image']->getmimeType();
$image_org = $post_array['image']->getClientOriginalName();
$response = $this->client->post(url('/update_profile'), [
'multipart' => [
[
'name' => 'image',
'filename' => $image_org,
'Mime-Type'=> $image_mime,
'contents' => fopen( $image_path, 'r' ),
],
]
]);