Problem to upload file with from-param in Laravel 8 external api with guzzle or HTTP client - api

I am using Mirakl api. I faced an issue with the file upload with form-peram in Laravel 8 external api with guzzle or HTTP client.I got 400 Bad Request.The api expects file with from-param like this
{
"order_documents": [{
"file_name": String,
"type_code": String
}]
}
The documentation of my endpoint can be found from this link (http://185.29.149.41/eci/mir/doc/api/OR74.html) and here is my codes --
public function upload_order(Request $request)
{
$order_id = $request->order_id;
if ($files = $request->file('files')) {
$name = $files->getClientOriginalName();
Storage::disk('invoice')->put($name, file_get_contents($files->getRealPath()));
$path = Storage::disk('invoice')->path($name);
}
$file['order_documents'][] = [
'file_name' => $name,
'type_code' => $request->type_code,
];
$json = json_encode($file);
$data = [
['name' => 'order_documents','contents' => $json],
];
$data[] = [
'name' => 'files',
'contents' => mb_convert_encoding($path, 'UTF-8', 'UTF-8'),
'filename' => $name,
];
$body['multipart'] = $data;
$configApi = $this->configApi($this->getMarketplaceID($request->marketplace), auth()->user()->id);
$url = $configApi['url'].'/api/orders/'.$order_id.'/documents';
$client = new Client(['verify' => false]);
$response = $client->request(
'POST',
$url,
[
'multipart' => $data,
'headers' => $configApi['headers']
]);
return json_decode($response->getBody(), true);
}

Related

i was trying to check api on postman using phalcon and i got respon 400

I got this error when i try to check using postman... u can see the error below
i got this massage
"code":400,"response":"Failed","message":"Your username is not registered. Please register a new user now!","data":null
public function loginAction()
{
$this->view->disable();
$credentials = $this->request->getJsonRawBody();
$response = new Response();
$username = $credentials->username;
$password = $credentials->password;
// $user = User::findFirst("user_username = '$username'");
$user = User::findFirst([
'conditions' => 'username = :username:',
'bind' => [
'username' => $username
]
]);
if($user !== NULL) {
$checkPassword = $this->security->checkHash($password, $user->password);
if($checkPassword === true) {
$this->session->set('username', [
'id' => $username->id,
'username' => $user->username,
]);

Rest Unknown Error while uploading image on wordpress media using guzzle

Rest Unknown Error while uploading image on wordpress media using guzzle:
Actually m trying to upload image using guzzle but the wordpress is giving server error that rest upload unknown error ... Please tell what i am doing wrong???
public function store(Request $request)
{
//
$image_path = $request->file('image')->getPathname();
$image_mime = $request->file('image')->getmimeType();
$image_org = $request->file('image')->getClientOriginalName();
$username = '####';
$password = '#######';
$http = new \GuzzleHttp\Client;
$response = $http->POST('websitenameurl/wp2/wp-json/wp/v2/media/',
[
'headers'=>[
'Authorization'=> 'Basic ' . base64_encode( $username . ':' . $password),
'Content-Type' => 'Application/json'
],
'multipart' => [
[
'name' => 'image',
'filename' => $image_org,
'Mime-Type'=> $image_mime,
'contents' => fopen($image_path, 'r' ),
],
],
]);
$result = json_decode((string)$response->getBody(),true);
return $result;
}

How to call an API using raw input using Guzzle

I have an API which is working well with Postman but upon trying to call it in code, I get errors. See below
In Postman
Below is how I am calling the API in code:
In Code
public function pay_bill(Request $request){
$client = new Client(); //GuzzleHttp\Client
$username = 'xxxx';
$password = 'xxx#2020*';
$credentials = base64_encode("$username:$password");
$transaction_id = intval($request->input('transaction_id'));
$amount = (int)$request->input('amount');
$bill_number = (int)$request->input('bill_number');
$return_url = $request->input('return_url');
$response = $client->post('https://gatewaytest.e.com:5000/gateway/initialize', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Basic ' . $credentials,
'X-API-KEY' => '7c4a8d09ca3762af61e59520943dc26494f8941b',
],
'form_params' => [
'transaction_id' => $transaction_id,
'total_amount' => $amount,
'bills' => ["bill_ref" => $bill_number, "amount" => $amount],
'return_url'=> $return_url
],
'verify' => true
]);
$transaction_data = $response->getBody();
return $transaction_data;
}
What am I doing wrong? How do I send body as raw like is done in postman? I imagine the problem could be due to using form_params
I finally found the solution as shown below:
'bills' => [["bill_ref" => $bill_number, "amount" => $amount]]

File upload using http request in Extbase, TYPO3 CMS

We want to upload file using http request to some api endpoint in TYPO3 9.
File upload can be done using \TYPO3\CMS\Core\Http\RequestFactory in
TYPO3
$filePath = '/var/www/html/MyTypo3Project/Image.png';
$username = 'test';
$password = 'test';
$multipart = [
// File Parameter
[
'name' => 'Image', //Api side parameter name
'contents' => fopen(realpath($filePath), 'r'),
'filename' => 'MyCustomName.png' // Custom filename
],
//Other Parameters
[
'name' => 'custom_param',
'contents' => 'custom_param_value'
]
];
// Request options along with auth header
$additionalOptions = [
'auth' => [$username, $password],
'multipart' => $multipart,
];
$requestFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Http\RequestFactory::class);
$response = $requestFactory->request(
$url, // Api Endpoint Url
'POST',
$additionalOptions // Passing the additional options
);

Laravel 5.1 Guzzle - Undefined offset: 0

I need to access an API so I use guzzle6 and I write a function:
public function test()
{
$client = new GuzzleHttp\Client(['defaults' => ['verify' => false]]);
try {
$res = $client->post('https://example.com/api/v2/oauth/token?grant_type=client_credentials', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'auth' => [
'Username' => 'user_5639',
'Password' => 'pass_asdhbas67yausihd7qaw8'
]
]);
$res = json_decode($res->getBody()->getContents(), true);
}
catch (GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$result = json_decode($response->getBody()->getContents());
return response()->json(['data' => $result]);
}
}
but I got error:
ErrorException in Client.php line 346: Undefined offset: 0
When I try at POSTMAN the same request everything is fine:
How to solve my problem?
If you have a look at the Guzzle Manual for the auth-option, you'll see it expects a numerically indexed array, with the username on index 0 and the password on index 1.
So this should work:
$res = $client->post('https://example.com/api/v2/oauth/token?grant_type=client_credentials', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'auth' => [
'user_xxxx', 'pass_xxxxx'
]
]);