dimanche 23 décembre 2018

Dingo API appends '%' character in the JSON response

im getting this weird result whenever im calling my REST API built in Dingo and Laravel 5.7

curl -X POST 'localhost/api/authenticate' -d "email=admin@example.com&password=secret"

everytime i call this code, it appends a "%" character on the end of JSON response, like

{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC90Z3AtZGVtby5vcGVuYWR1bHQub3JnXC9hcGlcL2F1dGhlbnRpY2F0ZSIsImlhdCI6MTU0NTYxMTg2OSwiZXhwIjoxNTQ1NjE1NDY5LCJuYmYiOjE1NDU2MTE4NjksImp0aSI6InR2M2NkdXpQSDh1VXpmaTgiLCJzdWIiOjEsInBydiI6Ijg3ZTBhZjFlZjlmZDE1ODEyZmRlYzk3MTUzYTE0ZTBiMDQ3NTQ2YWEifQ.pOUN-aiLq-78Rh429gvq3_S77L8gsHIh3oC_Bx41Y60"}%

causing the JSON object to be invalid. All it does is simply call response->json method

/**
 *  API Login, on success return JWT Auth token
 *
 * @param \Illuminate\Http\Request $request
 *
 * @return \Illuminate\Http\JsonResponse
 */
public function authenticate(Request $request)
{
    // grab credentials from the request
    $credentials = $request->only('email', 'password');
    try {
        // attempt to verify the credentials and create a token for the user
        if (!$token = JWTAuth::attempt($credentials)) {
            return response()->json(['error' => 'invalid_credentials'], 401);
        }
    } catch (JWTException $e) {
        // something went wrong whilst attempting to encode the token
        return response()->json(['error' => 'could_not_create_token'], 500);
    }
    // all good so return the token
    return response()->json(compact('token'));
}

am I missing something here?

ps. here is the link to the github repo:https://github.com/openadult/tgp/blob/df7d6819697801145e4537db8ce8ceec0fe9de9c/app/Http/Controllers/Api/AuthenticateController.php#L18-L32

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire