401 Unauthorized response while using PHP to access the API

I’m trying to access the Fibery API via PHP using the Leaf PHP 3.0 framework. I’ve first tested the API call I’m trying to make in Postman and got that working. Now I’m converting to php and running into an 401 Unauthorized response.

Am I missing something simple? If not I’ll dive into the support community of Leaf PHP.

Leaf docs can be found here: Leaf Fetch | Leaf PHP

The code (

$res = Fetch::request([
  "url" => 'https://myurl.fibery.io/api/commands',
  "method" => "POST",
  "headers" => [
    "Authorization" => 'Token my.token',
    "Content-Type" => "application/json"
  ],
  "data" => ['[{ "command": "fibery.schema/query" }]'],
]);

The response:

stdClass Object
(
    [data] => 
    [status] => 401
    [headers] => Array
        (
            [0] => HTTP/2 401
            [date] => Mon, 05 Feb 2024 14:41:57 GMT
            [content-length] => 12
            [x-kong-response-latency] => 6
        )

    [request] => Array
        (
            [url] => https://myurl.fibery.io/api/commands
            [method] => POST
            [baseUrl] => 
            [headers] => Array
                (
                    [Authorization] => Token my.token
                    [Content-Type] => application/json
                )
            [params] => Array
                (
                )
            [data] => Array
                (
                    [0] => [{ "command": "fibery.schema/query" }]
                )
            [timeout] => 0
            [withCredentials] => 
            [auth] => Array
                (
                )
            [responseType] => json
            [responseEncoding] => utf8
            [xsrfCookieName] => XSRF-TOKEN
            [xsrfHeaderName] => X-XSRF-TOKEN
            [maxContentLength] => 2000
            [maxBodyLength] => 2000
            [maxRedirects] => 5
            [socketPath] => 
            [proxy] => Array
                (
                )
            [decompress] => 1
            [rawResponse] => 
            [verifyHost] => 1
            [verifyPeer] => 1
            [curl] => Array
                (
                )
        )
)

The prime suspect would be your token, or something else about the auth header.

I would try inspecting/logging the Fetch::request arg, and looking for clues there.

1 Like

The syntax I used for passing the header options was wrong. Thanks for the suggestion Matt!

I solved it by using the Code snippet option in Postman to copy the curl command line version of the request. I added the verbose (-v) option that that. I then modified the Leaf Fetch library to also use the verbose mode (CURLOPT_VERBOSE = true).

I then compared both verbose outputs of the successful and unsuccessful calls and noticed the headers were not included. I then guessed the right syntax and got it working.

For anyone running into the same problem, here’s the correct way of passing the headers:

  $res = Fetch::request([
    "url" => 'https://myurl.fibery.io/api/commands',
    "method" => "POST",
    "headers" => [
      'Authorization: Token my.token',
      'Content-Type: application/json'
    ],
    "data" => '
      [{ "command": "fibery.schema/query" }]
    ',
  ]);
2 Likes