Rest assured - Can't reuse reponse token

375 views Asked by At

I need to create a requests for Rest testing. I've created some simple tests, login, create accound,... But now it's more complicated for me.

My example of a request looks like that:

    JSONObject childJSON = new JSONObject();
    childJSON.put("email", "[email protected]");
    childJSON.put("password", "1asdaasx");

    JSONObject request = new JSONObject();
    request.put("user", childJSON);
    
    String token = given().
        auth().
        basic("login", "password").
        header("Content-Type", "application/json").
        body(request).
     when().
        post("https://BASEuri.com/api/users/login").
     then().
        statusCode(200).
        .log().all()
        .extract().path("user.token").toString();

 }  
 

REPONSE { "user": { "username": "user", "email": "[email protected]", "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYwYWNmOGMzNjkzMTg1OWE1ZWVkNjQzMSIsInVsdcalIjoicGkyc3FyMiIsImV4cCI6MTYyNzY3ODE4MiwiaWF0IjoxNjIyNDk0MTgyfQ.a4kfaLbQgO0y-8MRx69O4SOSasdw4XCkMvhetDkG3w", "bio": "123", "image": "" } }

Now I need to take the token and pass it to the next request? According to spec

API SPEC

Update user PUT /api/user Example { "user":{ "email": "[email protected]", "bio": "I like music", "image": "https://i.stack.imgur.com/qwe.jpg" } }

Second request

JSONObject childJSONSub = new JSONObject();      
childJSONSub.put("email", "[email protected]");          
childJSONSub.put("bio", "My New Bio");       
childJSONSub.put("image", "https://i.stack.imgur.com/sad3.jpg");  

JSONObject requestSub = new JSONObject();        
requestSub.put("user", childJSONSub); 

given().            
    auth().             
    basic("login", "password").             
    header("Content-Type", "application/json").          
    queryParam("Authorization", token).             
    body(requestSub).        
when().             
    put("https://BASEuri.com/api/user").         
then().    
    log().all();

But the answer is like that:

"errors": {
    "message": "No authorization token was found",
    "error": {
        "name": "UnauthorizedError",
        "message": "No authorization token was found",
        "code": "credentials_required",
        "status": 401,
        "inner": {
            "message": "No authorization token was found"
        }
    }
}

Any suggestions? I've tried multiple solutions for token but always wi the same result.

1

There are 1 answers

1
Hanich On

You will need an access token to be authorized to access this feature

This access token is made available in different ways depending on the API

Probably there is some endpoint to get the token

In order to help you more, I would need more information about the problem