Why is my Authorization Header giving me a 401 in Guzzle?

后端 未结 2 1560
栀梦
栀梦 2021-01-20 06:46

I am getting a 401 on Guzzle 4.2 and the same setup works on Postman. Code below.

// Create a client with a base URL
    $client = new GuzzleHttp\\Client([\'         


        
2条回答
  •  鱼传尺愫
    2021-01-20 07:34

    Looking at the documentation page as per this line:

    $response = $client->get();
    

    It will send a get request, with no authorisation, hence the 401 response.

    Try the following instead

    // Create a client with a base URL.
    $client = new GuzzleHttp\Client();
    
    $request = $client-> createRequest('GET', 'cloud.feedly.com/v3/streams/contents?streamId=user/user-id/category/global.all&count=1');
    
    $request->setHeader('Authorization', "auth-code");
    
    // Send.
    $response = $client->send($request);
    
    dd($response->json());
    

    The above creates a request, set the authorisation header on it. Then once prepared it actually sends it.

    I think it worked in Postman because your headers are set, if you remove the authorization header it will likely fail there too.

    I have not tested this but think it will work.

提交回复
热议问题