0
votes

I am trying to send an post http request using Guzzle but i got error 500

Internal server error 
ServerException in RequestException.php line 113:
Server error: `POST https://domainname/api/v1/user/register` resulted in a `500 Internal Server Error` response:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex,nofo (truncated...)

I tried to send the same header and body keys using Postman , it worked without any issue :

{
    "status": "true"
}

Header: enter image description here Body: enter image description here

This is my Guzzle client request :-

        $client = new GuzzleHttp\Client(['base_uri' => 'https://domainname/api/v1/']);
                   $response = $client->request('POST', 'user/register', [
                       'headers' => [
                           'Authorization' => Session::get('token'),
                           'Content-Type'     => 'application/x-www-form-urlencoded',
                       ],
                       'form_params' => [
                           'start_date' => $start_date,
                           'expiry_date' => $expiry_date,
                           'type' => 'normal',
                           'status' => 'active',

                       ]
                   ]);

                    $response= $response->getBody();
                    $re= json_decode($response);

My route to that api:-

Route::post('/user/register', 'UsersAPI@register'); 

My Register controller :

public function register(Request $request){
Log::info('Request: ' . $request);
//dd($request->all());
     $validator = Validator::make($request->all(), [
        // 'user_id' => 'required',
        'start_date' => 'required',
        'expiry_date' => 'required',
        'type' => 'required',
        'status'=>'required',   
        //'receipt' => 'required',
        //'purchase_id'=>'required',
    ]);

    $user = JWTAuth::toUser(); 
      //Check whether validation is failed or passed
    if($validator->fails()){
        //Redirect back with validation errors
        return response()->json(['error' => $validator->errors()], 400);
    }



         $user = new User($data);
        $user->user()->save($user);
     Log::info('save_user done for this user: ' . $user->id);


    return ['status' => 'true']; 
}

NGINX error.log :-

[02/Dec/2017:15:49:13 +0300] "POST /api/v1/user/register HTTP/1.1" 500 16904 "-" "GuzzleHttp/6.2.1 curl/7.55.0 PHP/7.1.9"
1
In your Postman I can see you're missing space for Authorization header, between "Bearer" and the token itself. By that, take a look at the value of Session::get('token'). BTW Error 500 can mean anything. You need to deep and figure out the real problem. This log output is not so useful. - Dusan
@Dusan Yes you are right, i just tried with replacing the variables in request with 'string' , it worked without returning error . - Q8root

1 Answers

0
votes

If the same code produces different results depending on how you send your request, I would assume there are still differences between the request payloads.

Have you double-checked the contents of Session::get('token'), $start_date and $expiry_date?