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),
]);
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 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
I'm trying to make a GET request passing a bearer token as authentication.
I try to pass the token with:
$response = Http::get($url, [
'headers' => [
'Authorization' => 'Bearer ' . $token,
'Accept' => 'application/json',
],
]);
as stated in the docs
When I check the value of the variables, I get:
$token : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MDc0NTI0NzksInVzZXJuYW1lIjoic3VuY2hhaW4iLCJvcmdOYW1lIjoib3JnMCIsImlhdCI6MTYwNzQxNjQ3OX0.LUTFZh8Em13f3cQ8TpxgayVRC9XvVHyczOhQXARxk48"
$url : "https://example.com/channels/common/chaincodes/main?peer=org0/peer0&args=%5B%222020-12-01T22%3A00%3A00Z%22%2C+%222020-12-01T22%3A30%3A00Z%22%5D&fcn=GetMeasuresBetween"
But when I copy paste those values in Postman, GET is working and I can get my data, which means data is correct, and the way I execute my GET request might be incorrect.
Where am I wrong ? It seems all good to me !
I found the solution,
$response = Http::withToken($token)->get($url, [
'headers' => $headers,
'peer' => $this->peer,
'args' => $arrayArgs,
'fcn' => "GetMeasuresBetween",
]);
Or, you may use guzzle, and initialize
$client = new \GuzzleHttp\Client(['base_uri' => $this->url]);
$response = $client->request('GET', $url, [
'headers' => $headers,
'peer' => $this->peer,
'args' => $arrayArgs,
'fcn' => "GetMeasuresBetween",
]);
I don't know why it is not working the first way, but it is now working !
I can't find any example of how to download a remote file using Guzzle 6.0. I need to pass headers in the GET request.
I have looked at the documentation which isn't helpful at all.
I came up with this but it still doesn't download a file
require_once('vendor/autoload.php');
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
$client = new Client();
$response = $client->request('GET', '/stream/20', [
'headers' => [
'Authorization: Token token' => 'df456g4fd564gfs65dg45s6fdg4dsf5g4sd65g',
'Cache-Control' => 'no-cache',
'Content-Type' => 'application/pdf'
],
'sink' => 'https://example.com/path/to/file',
]);
Has anyone successfully downloaded a file using request headers?
I think you got confused.
Where you have '/stream/20' is the url of where the file is you want to download.
The sink part is where you want to save your image.
Try this....
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
$client = new Client();
$resource = fopen('test.pdf', 'w');
$response = $client->request('GET', 'https://example.com/path/to/file', [
'headers' => [
'Authorization' => 'Token token=df456g4fd564gfs65dg45s6fdg4dsf5g4sd65g',
'Cache-Control' => 'no-cache',
'Content-Type' => 'application/pdf'
],
'sink' => $resource,
]);
echo 'downloaded';
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' ),
],
]
]);