-1

I'm trying to POST data to a remote AWS API.

The data should be a JSON on the body part.

Using Postman, I can send the data and everything works correctly :

enter image description here

Now, trying to do so using GuzzleHttp\Psr7\Request, I'm doing :

$request = new \GuzzleHttp\Psr7\Request(
            'POST',
            'AWS API URL',
            ['Host' => 'AWS HOST', 'body' => '{"json": "my JSON"}']
        );
$request = $signer->signRequest($request, $credentials);
$response = $client->send($request);

The request succeed, but there is no data update ! As if the 'body' is not received.

I have no access to the remote API log files.

So my question, is this the correct way to post data in body part of a Guzzle request ?

Thanks.

Hamza Abdaoui
  • 2,029
  • 4
  • 23
  • 36
  • Does this answer your question? [How can I use Guzzle to send a POST request in JSON?](https://stackoverflow.com/questions/22244738/how-can-i-use-guzzle-to-send-a-post-request-in-json) – Jeto Dec 20 '19 at 09:54
  • @Jeto, I've edited my question, the `request` should be signed. So I should provide the data on the request object, not on the `client->send()`. At least, that's my analyses. – Hamza Abdaoui Dec 20 '19 at 10:10

1 Answers1

1

As per the linked answer, you need to pass the following options along with your request:

[GuzzleHttp\RequestOptions::JSON => ['key1' => 'value1', 'key2' => 'val2']] 

or:

['json' => ['key1' => 'value1', 'key2' => 'val2']]

But since you need to build your Request object first, you should be able to pass this option as the second parameter of Client::send:

$response = $client->send($request, [
  GuzzleHttp\RequestOptions::JSON => ['key1' => 'value1', 'key2' => 'val2']
];
Jeto
  • 14,596
  • 2
  • 32
  • 46