# 401 Unauthorized response while using PHP to access the API

**URL:** https://community.fibery.io/t/401-unauthorized-response-while-using-php-to-access-the-api/5803
**Category:** API & Programming
**Created:** [February 5, 2024, 2:48pm UTC](https://community.fibery.io/t/401-unauthorized-response-while-using-php-to-access-the-api/5803 "2024-02-05T14:48:00Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![tomvb](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/tomvb/32/8764_2.png) [@tomvb](https://community.fibery.io/u/tomvb)
#### Post date: [February 5, 2024, 2:48pm UTC](https://community.fibery.io/t/401-unauthorized-response-while-using-php-to-access-the-api/5803/1 "2024-02-05T14:48:00Z")

</div>

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](https://leafphp.dev/modules/fetch/#request-object)

The code (

```auto
$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:

```auto
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
                (
                )
        )
)

```

---

<div class="post-metadata">

### Author: ![Matt\_Blais](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/matt_blais/32/1464_2.png) [@Matt\_Blais](https://community.fibery.io/u/Matt_Blais)
#### Post date: [February 5, 2024, 5:13pm UTC](https://community.fibery.io/t/401-unauthorized-response-while-using-php-to-access-the-api/5803/2 "2024-02-05T17:13:48Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![tomvb](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/tomvb/32/8764_2.png) [@tomvb](https://community.fibery.io/u/tomvb)
#### Post date: [February 5, 2024, 6:36pm UTC](https://community.fibery.io/t/401-unauthorized-response-while-using-php-to-access-the-api/5803/3 "2024-02-05T18:36:53Z")

</div>

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:

```auto
  $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" }]
    ',
  ]);

```
