0

I'm trying to make a post http request using Illuminate\Support\Facades\Http.

My request :

$response = Http::post(
            $this->api.'/auth',
            [
                'auth' => [
                    'username' => $this->username,
                    'password' => $this->password
                ]
            ]
        )->json('response');

While api,username and password are set in a __construct method.

This request is working well in postman. But when I run it on php server I get a null. I digged in the response and I found the following :

Illuminate\Http\Client\Response {#2741 ▼
  #response: GuzzleHttp\Psr7\Response {#2766 ▼
    -reasonPhrase: "Not Acceptable"
    -statusCode: 406
    -headers: array:5 [▼
      "Server" => array:1 [▶]
      "Date" => array:1 [▶]
      "Content-Type" => array:1 [▶]
      "Content-Length" => array:1 [▶]
      "Connection" => array:1 [▶]
    ]
    -headerNames: array:5 [▼
      "server" => "Server"
      "date" => "Date"
      "content-type" => "Content-Type"
      "content-length" => "Content-Length"
      "connection" => "Connection"
    ]
    -protocol: "1.1"
    -stream: GuzzleHttp\Psr7\Stream {#2765 ▼
      -stream: stream resource @28 ▶}
      -size: null
      -seekable: true
      -readable: true
      -writable: true
      -uri: "php://temp"
      -customMetadata: []
    }
  }

Do you have an idea on how to solve this ?

Snowfire
  • 171
  • 9
  • As it's not our server, we're not likely able to help you. We have no idea what this server is expecting to receive or why it might be rejecting your request. – miken32 Aug 18 '21 at 23:18
  • There was some issues occuring in the server. I tried to switch to local and this solved my issue temporarily. – Snowfire Aug 19 '21 at 14:22

1 Answers1

0

You could try exporting the snippet as PHP code and see if you did not miss something out.How to get code snippet

YolloDev
  • 31
  • 7
  • I have the same values as postman, just checked ! I can't understand why it works on postman but doesn't on my php server ! – Snowfire Aug 18 '21 at 21:34
  • I am no PHP developer so I will just assume that this code is running on the client side. One of the reasons that come to my mind is CORS, could you check your console to see if there are any errors there? – YolloDev Aug 19 '21 at 22:20
  • Nothing gets desplayed in console. All I'm getting is a null response of my query. – Snowfire Aug 19 '21 at 22:57
  • It looks like you get a 406 status code which basically means that you do not specify the content type in the request. Try to find out the response (content type) returned by server and provide it in your request Accept header Provide this (content type) in your request Accept header. Check this link as well: https://en.wikipedia.org/wiki/List_of_HTTP_header_fields – YolloDev Aug 21 '21 at 23:18