I want to send a POST request to an external API
Here is my method to call
public function recursub(Request $request) {
$users = User::where('cancel_trial', 1)->get();
foreach($users as $user) {
//dd($user->email);
$url= 'https://api.paystack.co/subscription';
$client = new Client();
$response = $client->request('POST', $url, [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer sk_test_0994##############',
],
'form_params' => [
'customer' => '233########',
'plan' => 'PLN_###########',
]
]);
dd($response->getBody());
//dd($response->getBody()->getContents());
}
}
But I keep getting this error when I call the endpoint
GuzzleHttp \ Exception \ ClientException (400)
Client error: POST https://api.paystack.co/subscription resulted in a 400 Bad Request response: { "status": false, "message": "Request body could not be parsed. Make sure request body matches specified content-ty (truncated...)
It's looks like that you send wrong request to API.
If you set application/json maybe you should pass parameter in body, not in form params:
$response = $client->request('POST', $url, [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer sk_test_0994##############',
],
'body' => json_encode([
'customer' => '233########',
'plan' => 'PLN_###########',
])
]);
Another information that maybe can be helpful is that you can disable exception with ['http_errors' => false] option (http://docs.guzzlephp.org/en/stable/request-options.html#http-errors).
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 am trying to make a post request to a url that first requests for a username and password and then I provide the api_key to authorize myself in Laravel but without success. Can you please help?
$credentials = base64_encode('username1' .':' . 'password1');
try {
$defaultValues = [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Basic '. $credentials,
],
'auth' => 'Bearer ' . $api_key,
'verify' => false
];
$client = new Client($defaultValues);
$response = $client->post(config('myurl') . '/payments' . '/createPayment', ['json' => $data]);
Thanks
Change your code to this:
$credentials = base64_encode('username1' .':' . 'password1');
try {
$defaultValues = [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Basic '. $credentials,
],
'api_token' => $api_key,
'verify' => false
];
$client = new Client($defaultValues);
$response = $client->post(config('myurl') . '/payments' . '/createPayment', ['json' => $data]);
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 am trying to implement BlueSnap payment(Embedded Checkout) on my WebApp in Laravel 6.
https://developers.bluesnap.com/v8976-Tools/docs/embedded-checkout.
I allowed server and public IP to access the BlueSnap Sandbox routes
First, I am getting the token from BlueSnap and this work perfect.
https://developers.bluesnap.com/v8976-Tools/docs/create-embedded-checkout-token.
But then, when I request for card transaction (https://developers.bluesnap.com/v8976-JSON/docs/auth-capture), with "pfToken" which I get from previous request, I am getting 401.
public function makePayment(Request $request) {
$endpoint = 'https://sandbox.bluesnap.com/services/2/transactions';
try {
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $endpoint, [
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Basic dXNlcm5hbWU6cGFzc3dvcmQ='
],
'json' => [
'cardTransactionType' => 'AUTH_CAPTURE',
'amount' => '25.00',
'currency' => 'USD',
'pfToken' => $request->token,
],
]);
} catch (RequestException $e) {
dd($e);
}
dd($response);
}
I also tried to send request with Postman, and I got same error.
I need to integrate an API so I write function:
public function test() {
$client = new GuzzleHttp\Client();
try {
$res = $client->post('http://example.co.uk/auth/token', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'json' => [
'cliend_id' => 'SOMEID',
'client_secret' => '9999jjjj67Y0LBLq8CbftgfdreehYEI=',
'grant_type' => 'client_credentials'
]
]);
$res = json_decode($res->getBody()->getContents(), true);
dd($res);
}
catch (GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$result = json_decode($response->getBody()->getContents());
return response()->json(['data' => $result]);
}
}
as a responde I got message:
{"data":{"error":"invalid_clientId","error_description":"ClientId should be sent."}}
Now when I try to run the same url with same data in POSTMAN app then I get correct results:
What is bad in my code? I send correct form_params also I try to change form_params to json but again I got the same error...
How to solve my problem?
The problem is that in Postman you're sending the data as a form, but in Guzzle you're passing the data in the 'json' key of the options array.
I bet that if you would switch the 'json' to 'form_params' you would get the result you're looking for.
$res = $client->post('http://example.co.uk/auth/token', [
'form_params' => [
'client_id' => 'SOMEID',
'client_secret' => '9999jjjj67Y0LBLq8CbftgfdreehYEI=',
'grant_type' => 'client_credentials'
]
]);
Here's a link to the docs in question: http://docs.guzzlephp.org/en/stable/quickstart.html#sending-form-fields
Also, I noticed a typo - you have cliend_id instead of client_id.