1

This code is giving timeout error whereas the service giving response in postman/soapUI

I am trying to automate the rest service. The service working fine soapUI whereas when automating in restAssured giving timeout error.

import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;

import io.restassured.RestAssured;
import io.restassured.response.Response;

public class AddUsers {

    @Test

    public void addUsers()

    {

        RestAssured.baseURI = "http://reqres.in";

        given().header("Content-Type","application/json").body("{\r\n" + 
                "    \"name\": \"Mallik\",\r\n" + 
                "    \"job\": \"TestLead\"\r\n" + 
                "}").when().post("/api/users");



    }


}

3 Answers3

0

You need to use https. i am getting the output correctly

    RestAssured.baseURI = "https://reqres.in";
    Response resp = given().header("Content-Type", "application/json")
            .body("{\n" + "    \"name\": \"Mallik\",\n" + "    \"job\": \"leader\"\n" + "}").when()
            .post("/api/users");
    System.out.println(resp.getStatusCode());
    System.out.println(resp.asString());
Arun Nair
  • 425
  • 3
  • 11
0

Try using the below. I have added code for addUsers and getUsers. I'd suggest you to create POJO class for the body you are passing the POST endpoint.

import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import net.serenitybdd.rest.SerenityRest;

public class Test11 {

    static Response response;

    @Test
    public void addUsers() {
        RestAssured.baseURI = "http://reqres.in";
        response = SerenityRest.given().urlEncodingEnabled(false).relaxedHTTPSValidation().contentType(ContentType.JSON)
                .log().all()
                .when().body("{\r\n" + 
                        "    \"name\": \"Mallik\",\r\n" + 
                        "    \"job\": \"TestLead\"\r\n" + 
                        "}")
                .post("/api/users")
                .then().log().all().extract().response();
    }

    @Test
    public void getUsers() {
        RestAssured.baseURI = "http://reqres.in";
        response = SerenityRest.given().urlEncodingEnabled(false).relaxedHTTPSValidation().contentType(ContentType.JSON)
                .log().all()
                .when()
                .get("/api/users?id=3")
                .then().log().all().extract().response();
    }
}

Let me know, if this solved your problem.

avidCoder
  • 440
  • 2
  • 10
  • 28
0

I had a same issue, it got resolved once I configured the proxy hostname and port as follows:

RestAssured.given(restAssuredSpecBuilder.build()).proxy("http.proxy.domain.com",portNumber)
Anand Krish
  • 431
  • 4
  • 4