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".
Related
I am trying to send url as attachment using Send Email api in Mailgun. But the attachment is not attached. I am using Laravel HTTP client
$params = [
'from' => $selected_options['mg_from_email'],
'to' => $selected_options['mg_to_email'],
'subject' => $selected_options['mg_subject'],
'text' => $selected_options['mg_text'],
'html' => $selected_options['email_body'],
];
$response = Http::asForm()->withOptions(['verify' => config('constants.verify_ssl')])->timeout(30)->withHeaders(
[
'Content-Type' => 'multipart/form-data',
'Authorization' => 'Basic ' . base64_encode('api:'.$api_key)
]
)->attach(
'attachment', file_get_contents('https://picsum.photos/200'), 'photo.jpg'
)->post(
$request_url,
$params,
);
Can anyone suggest what i am doing wrong here?
There is no error in response and the email is also sent successfully but the attachment is not there.
I am trying to use https://doc.doppio.sh/guide/cookbook/protected-url.html to convert a html to pdf. On this one, since my url is password protected I am having it difficult to print the page.
My code:
$client = new \GuzzleHttp\Client();
$response = $client->post('https://api.doppio.sh/v1/render/pdf/sync', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer apikey',
'Content-Type' => 'application/json'
],
'json' => [
'page' => [
"pdf" => [
"printBackground" => true
],
"goto" => [
"url" => 'https://example.com/admin/property'
],
"authenticate" => [
"username" => 'user#domain.com',
"password" => 'password'
],
]
]
]);
$responseBody = $response->getBody();
This is printing the page, however instead it is printing the login page. Its because when user is not authenticated, the system will redirect user to the login page.
Here, it has also mentioned about the https://doc.doppio.sh/guide/cookbook/protected-url.html#using-cookies cookie methods,however, I don't know what cookie should I pass on which name.
Your problem is more related to how does the login/auth works on the page you try to convert.
The "authenticate" prop you use here is related to HTTP Basic Authentication and I am not sure its what the page you want to access is using.
If you manually login into the page, check your browser, what is happening ? Do you see cookies ?
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?
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.
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.