0

I have got a Laravel Application which should send a POST request with parameters to the WebUntis API and the response should be a session id. I can send a POST request and I get a answer but I do not get the session key. I have tested the code in Postman and there it works.

route/api.php

Route::get('auth', 'UntisController@auth');

UntisController.php

class UntisController extends Controller
{

  public function auth()
  {

    $client = new \GuzzleHttp\Client();
    $req = $client->post( 'https://asopo.webuntis.com/WebUntis/jsonrpc.do?school=htblva_villach',[

                "id" => 8294829,
                "method" => "authenticate",
                "params" => [
                    "user" => "USERNAME",
                    "password" => "PASSWORD",
                    "client" => "web"
                ],
                "jsonrpc" => "2.0"
    ],
    ['Content-Type' => 'application/json'
    ]);



    dd($req);
  }
}

the response I get with POSTMAN, the respones i want

my Postman request

The response I get from my code

I have tried it already with

   dd($req->getBody());

but I do not get the sessionid i want.

Chamara Abeysekara
  • 1,272
  • 1
  • 10
  • 28
  • Whose server is it? Do you have control over the API, if you don't, I don't think there is a way to get something that the API does not send, unless there is a different endpoint to get the session_key you want – Dhiraj May 03 '18 at 17:56
  • I do not own the API but if I make a Request over Postman with the same parameters I get the information I want. –  May 03 '18 at 17:58
  • Can you post the params you send in postman vs your script, and the response you recieve in postman vs your script ? – Dhiraj May 03 '18 at 18:02
  • I have uploaded a photo of all requests and responses –  May 03 '18 at 18:08
  • 3
    Possible duplicate of [Guzzlehttp - How get the body of a response from Guzzle 6?](https://stackoverflow.com/questions/30549226/guzzlehttp-how-get-the-body-of-a-response-from-guzzle-6) – ceejayoz May 03 '18 at 18:10
  • It's working fine, I think. There's just some extra steps in Guzzle to use the response. See the thread I've linked. – ceejayoz May 03 '18 at 18:11
  • Try doing `dd($req->getBody());` – Dhiraj May 03 '18 at 18:17
  • tream {#459 ▼ -stream: stream resource @288 ▼ wrapper_type: "PHP" stream_type: "TEMP" mode: "w+b" unread_bytes: 0 seekable: true uri: "php://temp" options: [] } -size: null -seekable: true -readable: true -writable: true -uri: "php://temp" -customMetadata: [] } –  May 03 '18 at 18:31
  • That is the answer I get, there is no sessionid –  May 03 '18 at 18:32

1 Answers1

0

You can try this method.(Send post request with api in laravel)

public function auth(Request $request)
{
    $token = session('token'); //if token needed you can take from session else you can remove token
    $body =  [
        "id" => 8294829,
            "method" => "authenticate",
            "params" => [
                "user" => "USERNAME",
                "password" => "PASSWORD",
                "client" => "web"
            ],
            "jsonrpc" => "2.0"
    ];

    $request = 
    Request::create('https://asopo.webuntis.com/WebUntis/jsonrpc.do? 
    school=htblva_villach') , 'POST', $body);
    $request->headers->set('Accept', 'application/json');
    $request->headers->set('Authorization', 'Bearer ' . $token); // if not required token you can comment this line
    $res = app()->handle($request);
    $req = json_decode($res->getContent()); //convert to json object

    dd($req);
}