-1

I am trying to pass array in post request payload.

url = http://IP:PortNo/index.php/v1/login

payload:

data={ "terminal_id": "terminal_id", "api_login": "api_login", "api_key": "api_key", "merchant_code": "merchant_code" }

what i did is:

$data = ['terminal_id' => $this->terminal_id , 'api_login' => $this->api_login,
        'api_key' => $this->api_key , 'merchant_code' => $this->merchant_code];
$response = $this->client->post($url, array('data' => $data));

but the response i am getting is:

"Request data not found"

this means my data payload is not correctly formed. any idea?

Faiz
  • 51
  • 3
  • 12
  • Have you tried encoding the $data to json with json_encode? I've done things like `[ 'data' => json_encode($data) ]` in the past with guzzle to have the right body sent. – Guillaume Rochat Jul 07 '17 at 12:22
  • yes i didi `j$response = $this->client->post( $url, ['data' => json_encode($data) ] );` but i get the same result `Request data not found` – Faiz Jul 07 '17 at 12:53
  • 1
    Maybe it's in the form of `$this->client->post($url [ 'body' => json_encode([ 'data' => $data ]) ]);` ? Maybe have a look at this : https://stackoverflow.com/questions/22244738/how-can-i-use-guzzle-to-send-a-post-request-in-json – Guillaume Rochat Jul 07 '17 at 12:56

1 Answers1

6
$response = $this->client->post($url, ['json' => $data]);

See the Guzzle6 manual in the section entitled json: http://docs.guzzlephp.org/en/stable/request-options.html#json

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Alexey Shokov
  • 4,775
  • 1
  • 21
  • 22