moses toh
moses toh

Reputation: 13172

How to get response from guzzle use auth and body raw?

I try in postman like this :

I fill just input password. Then I click button update request

The view like this :

enter image description here

This is header :

enter image description here

This is body. I select raw and input data like this :

enter image description here

Then I click button send and it can get the response

But when I try use guzzle 6 like this :

public function testApi()
{
    $requestContent = [
        'headers' => [
            'Accept' => 'application/json',
            'Content-Type' => 'application/json'
        ],
        'json' => [
            'auth' => ['', 'b0a619c152031d6ec735dabd2c6a7bf3f5faaedd'],
            'ids' => ["1"]
        ]
    ];
    try {
        $client = new GuzzleHttpClient();
        $apiRequest = $client->request('POST', 'https://myshop/api/order', $requestContent);
        $response = json_decode($apiRequest->getBody());
        dd($response);
    } catch (RequestException $re) {
          // For handling exception.
    }
}

The result is empty

How can I solve this problem?

Upvotes: 1

Views: 909

Answers (1)

louisfischer
louisfischer

Reputation: 2084

See in Postman, you correctly specify the field Authorization in the "headers" tab. So it sould be the same when you use Guzzle, put it in the headers:

public function testApi()
{
    $requestContent = [
        'auth' => ['username', 'password']
        'headers' => [
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
        ],
        'json' => [
            'ids' => ["1"]
        ]
    ];

    try {
        $client = new GuzzleHttpClient;
        $apiRequest = $client->request('POST', 'https://myshop/api/order', $requestContent);
        $response = json_decode($apiRequest->getBody());
        dd($response);
    } catch (RequestException $re) {
          // For handling exception.
    }
}

Upvotes: 1

Related Questions