Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot add Autorization header to my requests #39

Closed
ksbomj opened this issue Aug 3, 2014 · 1 comment
Closed

Cannot add Autorization header to my requests #39

ksbomj opened this issue Aug 3, 2014 · 1 comment

Comments

@ksbomj
Copy link

ksbomj commented Aug 3, 2014

Hi guys, I have following code:

Client class

<?php namespace Solidoptics;

use GuzzleHttp\Client;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\Description;

class ApiClient extends Client
{
    public static function factory($config = array())
    {
        $client = new Client();

        $config = array_merge($config, json_decode(file_get_contents(__DIR__.'/Resources/config/client.json'), TRUE));

        $guzzleClient = new GuzzleClient(new Client(), new Description($config));

        return $guzzleClient;
    }
}

Config settings

   {
    "operations": {
        "me":{
            "description": "Returns user profile details",
            "httpMethod": "GET",
            "uri": "users/me",
            "parameters": {
                "token": {
                    "type":"string",
                    "location": "header",
                    "sentAs": "Authorization"
                }
            }
        }
    }
}

Usage

$config = [
    'baseUrl' => 'http://localhost/api/v1/'
];
$api_client = ApiClient::factory($config);

$token   = 'Bearer V6tBMG6FKL7wLxZh116IGdvfDUtOvlFIJI61nrHp';
$result = $api_client->me(array(
    'token' => $token
));

And I want add custom Autorizarion Bearer header to all requests, how I can do that? Add header to $client doesn't help cuz $result after call me method is NULL.

Thanks

@ksbomj
Copy link
Author

ksbomj commented Aug 5, 2014

Sorry guys, missed something in documentation so for me fix is in json change:

{
    "operations": {
        "me":{
            "description": "Return user profile details",
            "httpMethod": "GET",
            "uri": "users/me",
            "responseModel": "getResponse",
            "parameters": {
                "token": {
                    "type":"string",
                    "location": "header",
                    "sentAs": "Authorization",
                    "required": true
                }
            }
        }
    },
    "models": {
        "getResponse": {
            "type": "object",
            "additionalProperties": {
                "location": "json"
            }
        }
    }
}

@ksbomj ksbomj closed this as completed Aug 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant