0

When I do this on the commandline:

curl -X POST -d '{"userDetails":{"username":"myself","password":"Myself123"}}' https://sub.domain.com/energy/api/login

it returns a sessionid like expected.

When I use Guzzle 6 on Laravel 5.5 and do this:

$client = new GuzzleHttp\Client();
    $login = $client->post('https://sub.domain.com/energy/api/login', [
        'userDetails' => [
            'username' => 'myself',
            'password' => 'Myself123'
        ]
 ]);

I get this error:

Server error: `POST https://sub.domain.com/energy/api/login` resulted in a `503 Service Unavailable` response: org.json.JSONException: JSONObject["userDetails"] not found.

What am I doing wrong?

Klaaz
  • 1,590
  • 3
  • 23
  • 46
  • 1
    It's probably that you're not sending it as JSON content in your Guzzle example. [See here](https://stackoverflow.com/questions/22244738/how-can-i-use-guzzle-to-send-a-post-request-in-json) for how to. – scrowler Sep 27 '17 at 00:34
  • Possible duplicate of [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) – scrowler Sep 27 '17 at 00:34

2 Answers2

0

Use this to debug and send request, sample here.

 include these in class
//FOR GUZZLE
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\ClientException;


try{
        $endPoint = "http://server_ip/security/subject";
        $options = [ "auth" =>  [$username, $password], "headers" => ["header1" => value, "header2" => value] ];

        $client         = new Client();
        $response       = $client->get($endPoint, $options);
        $responseArray   = json_decode($response->getBody()->getContents(), true);
        $status = $response->getStatusCode();



    }catch(BadResponseException $e){

        $response           = $e->getResponse();
        $reason             = $response->getReasonPhrase();
        $exceptionMessage   = (!empty($reason)) ? $reason. ' email or password.' : 'Unauthorized email or password.';

    }
0

You are not passing as a json. Try this:

$req = [
    'userDetails' => [
        'username' => 'myself',
        'password' => 'Myself123'
    ]
];

$client = new GuzzleHttp\Client();
$login = $client->post('https://sub.domain.com/energy/api/login',
    [
        'json' => $req
    ]
);
return $login;
user3785966
  • 2,578
  • 26
  • 18