2

I'm trying to test an API post call in my laravel site, and I'm getting that the user doesn't exist because I believe the payload/body isn't being passed

If I test this manually in Swagger (with success) using the id and the body as

{
  "password": "passwordTest",
  "email": "[email protected]"
}

then the url shows as http://ip.address/login/?id=0 and the curl is

curl -X POST "http://ip.address/login/?id=0" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"password\": \"passwordTest\", \"email\": \"[email protected]\"}"

In my service file, it should be matching all the constructs but I dump it to the page and I get the error without a payload (the body)

Am I missing something?

AuthService.php

public function loginGetToken(){
    $password = "passwordTest";
    $email = "[email protected]";
    $id = "0";

    $payload = (object)[
        'password'=>(string)$password,
        'email'=>(string)$email
    ];

    $retval = $this->post('http://ip.address/login/?id='.$id,$payload);

    return $retval;
}

private function post($endpoint,$payload){
    //throws various exceptions:
    $result = $this->guzzleClient->request('POST', $endpoint, ['json'=>$payload]);
    $body = $result->getBody();
    return  json_decode($body);
}
1
  • //throws various exceptions can you post the exceptions you are getting? Commented Dec 18, 2018 at 17:35

1 Answer 1

1

It's been a while, but I believe you need to tell Laravel that it is getting json.

private function post($endpoint,$payload){
   $options = [
        'headers' => [
            'Accept'       => 'application/json',
            'Content-Type' => 'application/json',
        ],
        'json' => $payload
    ];
    //throws various exceptions:
    $result = $this->guzzleClient->request('POST', $endpoint, options);
    $body = $result->getBody();
    return  json_decode($body);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I actually got it to work, but this is a great answer because it ensures the JSON type just in case. Thanks!
@TomN. You should post your answer for anybody who comes after you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.