0

I have issue with Guzzle POST request

here's my code

$req = $client->request('POST', $endpoint, [
      'headers' => [
          'Content-Type' => 'application/json',
          'Authorization' => 'Basic '.$this->api_credential
         ], $body);

its always response 401 "Please include your API key as an Authorization header"

do i have to encode base64 the credential ?

and its using basic authorization

5
  • 2
    What are the specifications of the endpoint? Commented Nov 18, 2019 at 5:18
  • 1
    what credentials are you talking about? what does the API documentation say it wants? Commented Nov 18, 2019 at 5:20
  • The solution will be based on your API provider's specs. Can you link to a reference guide from this API provider? Commented Nov 18, 2019 at 5:28
  • im sry for the late answer here's the docs docs.xendit.co/postman Commented Nov 18, 2019 at 5:59
  • 1
    added an answer for you, good luck with your project Commented Nov 18, 2019 at 6:13

3 Answers 3

1

Based on the API you are trying to authenticate with you will need to base64_encode the credentials for Basic authentication (username:password)[where username is your API Secret Key and password will be blank]:

For the Authorization header:

'Authorization' => 'Basic '. base64_encode($this->api_credential .':'),
Sign up to request clarification or add additional context in comments.

Comments

0

Use Bearer instead of Basic authorization. Hope it will solve the issue

$req = $client->request('POST', $endpoint, [
      'headers' => [
          'Content-Type' => 'application/json',
          'Authorization' => 'Bearer '.$this->api_credential
         ], $body);

Comments

0

Please make sure that your server accept authorisation header. .htaccess code to accept authorisation header is given below

RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

You can check if the server accept authorisation code by using the following curl request

$ch = curl_init();  
curl_setopt( $ch,CURLOPT_URL,'https://yourendpoint.com/apiFile.php'  );
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Basic ' .$api_credential
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);    
$returnData = json_decode($result, true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.