Authorization in POST method using rest assured

385 views
Skip to first unread message

mohan...@gmail.com

unread,
Aug 3, 2018, 1:55:42 AM8/3/18
to REST assured
I'm trying to send a POST method to my API and it returns 401 error instead of 200 OK.

Here is the thing:

Using Postman: POST request works fine.

Under Authorization section, I have selected `Basic Auth` and have provided Username/pwd.

Under Headers section, I have given `X-application` and `Content-Type`.
My body in Postman is :

   
 {
     
"description": "TestAPIPostmannolast",
     
"issue_type": {
       
"id": "271341877549072389",
       
"name": "Design"
     
},
     
"area": {
       
"id": "271341877549072406"
     
},
     
"issue_id": "9d5ac7da-9626-11e8-9eb6-529269fb1459"
   
}


Using RestAssured: It fails with a 401 error.

   
package basic;
   
   
import io.restassured.response.Response;
   
import org.testng.annotations.BeforeClass;
   
import org.testng.annotations.Test;
   
import io.restassured.RestAssured;
   
import static io.restassured.RestAssured.given;
   
   
public class GetRequest {
   
   
       
@BeforeClass
       
public void setup() {
   
           
RestAssured.baseURI = "https://123.com";
   
           
RestAssured.basePath = "/field-management/api";
   
           
RestAssured.authentication =    RestAssured.basic("username", "password");
   
       
}
   
       
@Test
       
public void responseBody(){
   
           
Response response = given()
                   
.header("X-application", "63fc4887-aed9-497f-bad5-d7ef2b90cdaf")
                   
.contentType("application/json")
                   
.body("{\n" +
                           
"  \"description\": \"Intellij  \",\n" +
                           
"  \"issue_type\": {\n" +
                           
"    \"id\": \"271341877549072389\",\n" +
                           
"    \"name\": \"Design\"\n" +
                           
"  },\n" +
                           
"  \"area\": {\n" +
                           
"    \"id\": \"271341877549072406\"\n" +
                           
"  },\n" +
                           
"  \"issue_id\": \"2dd8ae7a-966e-11e8-9eb6-529269fb1459\n" +
                           
"\n\"\n" +
                           
"}")
   
                   
.when()
                   
.post("/projects/1879048400/areas/271341877549072406/issue/");
   
           
System.out.println(response.body().asString());
       
}



Response:

   
 {"message":"Authentication Failed","errorKey":null}


Not sure why is it not working when I try it through RestAssured.

Ngoc Tram Nguyen Thi

unread,
Aug 6, 2018, 5:23:35 AM8/6/18
to rest-a...@googlegroups.com
I guest you are using Interceptor on Postman. I am facing this problem now.

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Saurabh Garg

unread,
Aug 6, 2018, 7:14:56 AM8/6/18
to rest-a...@googlegroups.com
Can you try to pass username and password in cookies and check
Thanks,
Saurabh


--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.

Riyas Valiya

unread,
Sep 1, 2018, 11:03:36 AM9/1/18
to REST assured
It is possible that the charset might be the issue, follow the same suggestion Johan mentioned here and see if that makes a difference.
You could fire up wireshark and see what goes on the wire level for both Postman and REST-assured

Cheers,
Riyas
Reply all
Reply to author
Forward
0 new messages