How to make this postman request with laravel httpClient - laravel

I want to make a postman request with Laravel httpClient,the request in postman is like this:
The Content-Type in the header is: application/x-www-form-urlencoded
I tried many ways to make this request with Laravel httpClient, but it either return null or return all of the datas, not the data I am looking for.
I tried this:
$res = Http::withHeaders([
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Basic XXXXXXXXXX'
])->withBody(http_build_query(["uuid"=>$uuid]),"application/json")->post("https://xxx.com.cn/entity.find");
and also tried this:
$res = Http::withHeaders([
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Basic ZHh5LWVpbnRlbGxpZxxxxxxxx'
])->post("https://xxxx.com.cn/entity.find", [
"form_params" => [
"filter" => ['uuid'=>$uuid],
]
]);
All are not work. Could anyone give me some tips about this?

Related

Laravel EBay API Client Credential Grant Request Gives Error, Unsupported Grant Type

I am using Laravel 8 and the Http Client library. Here is my code:
public function mintNewApplicationAccessToken()
{
$response = Http::withHeaders([
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Basic ' . base64_encode(config('ebay.ebay_client_id_sandbox') . ":" . config('ebay.ebay_client_secret_sandbox')),
])->post(config('ebay.ebay_token_request_endpoint_url_sandbox'), [
'grant_type' => 'client_credentials',
'scope' => urlencode(config('ebay.ebay_client_credentials_scopes_sandbox')),
]);
dd($response->json());
}
I have double checked my client_id and client secret, I am using the Ebay sandbox at the moment, have checked the sandbox url is correct, I can't figure out what is wrong with my request? I get a 400 error back saying unsupported_grant_type and the error description says grant type in request is not supported by the authorization server. I have checked my scopes and everything seems in order?
I finally figured it out. As per the documentation on the Laravel website, I changed the code from:
public function mintNewApplicationAccessToken()
{
$response = Http::withHeaders([
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Basic ' . base64_encode(config('ebay.ebay_client_id_sandbox') . ":" . config('ebay.ebay_client_secret_sandbox')),
])->post(config('ebay.ebay_token_request_endpoint_url_sandbox'), [
'grant_type' => 'client_credentials',
'scope' => urlencode(config('ebay.ebay_client_credentials_scopes_sandbox')),
]);
dd($response->json());
}
To:
$auth = base64_encode(config('ebay.ebay_client_id_sandbox') . ':' . config('ebay.ebay_client_secret_sandbox'));
$response = Http::asForm()->withOptions([
'debug' => true,
])->withHeaders([
'Authorization' => 'Basic ' . $auth,
])->post(config('ebay.ebay_token_request_endpoint_url_sandbox'), [
'grant_type' => 'client_credentials',
'scope' => config('ebay.ebay_client_credentials_scopes_sandbox'),
]);
dd($response->json());
So. I added asForm to the code rather than the Content-Type line, and removed the url encode from the scopes.
What surprised me most was when I removed the url encode method from the scopes it started working? Very strange.
i have also same this problem and solution is that your account is not veriyfied or still waiting for aproval kindly contact support.

Laravel not making Guzzle HTTP POST request correctly

I am working on a megento integration and trying to get the admin access token by making a post request with form data. I tested the route out on Postman and it worked correctly:
However, when I tried to implement the same request in Laravel with Guzzle Http Client, it seem just cannot make the request properly as if the form data post body is not being recognized, and it keeps showing me errors saying the field values are required. Here is my request:
$client = new \GuzzleHttp\Client();
$response = $client->post($request['magento_domain'] . '/rest/V1/integration/admin/token', [
'form_params' => [
'username' => $magento_admin_username,
'password' => $magento_admin_password
], [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]
]);
and then this is the error I keep getting:
Update: I had also tried the request like this, it throws the same error:
$response = $client->post($request['magento_domain'] . '/rest/V1/integration/admin/token', [
'form_params' => [
'username' => $magento_admin_username,
'password' => $magento_admin_password
]
]);
I would appreciate any help!
The "Content-Type: application/json" request header is incorrect when sending a form-data body.
Just remove it, Guzzle automatically adds the correct Content-Type when using "form_params". Just JSON is wrong because the body is obviously not JSON.
I am using a JSON request in production successfully:
$res = $this->client->request('POST', 'https://.../rest/V1/integration/admin/token', [
'headers' => [
'Accept' => 'application/json',
'content-type' => 'application/json'
],
'json' => [
'username' => config('app.shopUser'),
'password' => config('app.shopPw')
]
]);
Or try using "multipart" instead of "form_params" - this should send a multipart/form-data request which is what Postman means with "form-data".
"form_params" is equivalent to "x-www-form-urlencoded".

PROBLEM with HEADER for test API Jetstream Sanctum Laravel 8x with PERMISSION

i generated two tokens:
tokenA = As2 ... xxxxx //can perform ONLY create
tokenB = Bs2 ... xxxxx //can perform ONLY update
i have the following problem
$response = $this->withHeaders([
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$tokenA],
])->post('/api/store',$data);
$response->assertStatus(201);
//the store is made without problems
$response = $this->withHeaders([
'Accept' => 'application/json',
'Authorization' => 'Bearer '.tokenB,
])->put('/api/update',$dataUpdate);
$respone->assertStatus(200);
//the test fails and returns 403. As if you don't have permission to do that
while if I call only
$response = $this->withHeaders([
'Accept' => 'application/json',
'Authorization' => 'Bearer '.tokenB,
])->put('/api/update',$dataUpdate);
$response->assertStatus(200);
the update is performed without problems.
How can I run the store and then the update in sequence?
it appears that $response continues to hold the value of tokenA

Getting null when posting data using Guzzle Client in Laravel

Am working on an a Laravel application whereby am posting some data to an API using Guzzle Http Client. The APIhas passport authentication which requires the token of the authenticated user to be passed on the headers. The headers also accept application/json as content-type and accept type.
I am also passing the data via POST request
The problem is that I keep getting a null response.
public
function post_policies($data, $token, $url) {
$client = new Client();
$serverURL = 'http://localhost/digital-apps-apis/public'.
'/'.$url;
$body = $client - > request('POST', $serverURL, $data, [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Bearer '.$token,
]) - > getBody();
$contents = $body - > getbody() - > getContents();
$data = json_decode($contents);
dd($data);
}
$body = $client->request('POST', $serverURL , $data, [
'debug' => true, //switch it to false before go live
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $token,
]]
)->getBody();
You must define your headers in the headers array.
Since it's a post i don't see you actually sending any data in the body. But if you do use the form_params array exactly like i used the headers array.

Datatable from lumen api to laravel application not working

I want to send the result of return Datatables::of(DB::table('ride'))->make(true); from lumen api to laravel application called by GuzzleHttp\Client client. I can display data in laravel application but can't sort or search. The request is going to the API but nothing is happening.
I think the request sent by Guzzle client is not the being received by the Lumen API :
API code :
public function rides(Request $request){
return Datatables::of(DB::table('ride'))->make(true);
}
Laravel Application code :
$resultat = $client_ppm->request('get', $url_, [
'form_params' => $datafield_array,
'headers' => [
'Authorization' => 'Bearer ' . $token,
'Content-type' => 'application/json',
'Accept' => 'application/json',
'from' => 'portail'
]
]);
return $resultat->getBody();

Resources