Getting request info as cURL from Rest-Assured.

Serhat Ozdursun
2 min readDec 26, 2022

--

Hello fellows,

In this article, I will explain how you can redirect Rest-Assured logs to log4J and generate a cURL from logs to reproduce the same request in your local.

To do this, I utilize the io.restassured.filter class of Rest-Assured. Once we implement the filter interface to the filter class, we will able to reach all request logs includes request logs.

Once you implement the filter interface you will have the following method.

@Override
public Response filter(FilterableRequestSpecification reqSpec, FilterableResponseSpecification resSpec, FilterContext filterContext) {

return null;
}

The arguments of method will help you to fetch request details. Let's start with the response to get the response code first.

var response = filterContext.next(reqSpec, resSpec);

Now we have possession of the response in all details. I start with the response because I want to log the request according to the desired response status code. Also, I want to able to use this filter with desired status code and without it. You know flexibility is the best.

So I created two contractor methods like the below to update the global desiredStatus variable.

public RestReqFilter(int statusCode) {
desiredStatusCode = statusCode;
}

public RestReqFilter() {
desiredStatusCode = 0;
}

Now we can decide whether the response is the expected response or not. Or we can log the response directly as info if the desiredStatusCode is not specified

if (desiredStatusCode != 0 && desiredStatusCode != response.statusCode())
log.error("""
Response code: {}
Response Body:
{}
You can use following cURL to reproduce the same request
{}""", response.statusCode(), response.asPrettyString(),
createCurl(reqSpec.getMethod(), reqSpec.getURI(), reqSpec.getHeaders(), reqSpec.getBody()));
else
log.info("""
Response code: {}
Response Body:
{}
You can use following cURL to reproduce the same request
{}""", response.statusCode(), response.asPrettyString(),
createCurl(reqSpec.getMethod(), reqSpec.getURI(), reqSpec.getHeaders(), reqSpec.getBody()));

Let’s dig into the createCurl method. Actually, it is just a string formatting method to generate a cURL from the logs

private String createCurl(String method, String url, Headers headers, String body) {
StringBuilder curl = new StringBuilder(String.format("%ncurl --location --request %s %s \\\n", method, url));

if (headers.size() > 1) {
headers.asList().forEach(h -> curl.append(String.format("--header '%s'\\\n",
h.toString().replaceFirst("=", ":"))));
}
if (body != null && !body.isEmpty() && !body.isBlank() && !body.equals("null")) {
curl.append(String.format("--data-raw '%s'", body));
}
return curl.toString();
}

As you can see generating a cURL is pretty easy when you have the required data of the request.

All that’s left is to add this filter class as a custom filter to your Rest-Assured request.

var fileHelper = new FileHelper();
int desiredStatusCode = 200;
var payload = fileHelper.getFile("pet.json");

given()
.basePath("{version}/pet")
.pathParam("version", "v2")
.filter(new RestReqFilter(desiredStatusCode))
.contentType(ContentType.JSON)
.body(payload)
.when()
.post()
.then()
.statusCode(desiredStatusCode);

See how can you use the cURL to reproduce the request if you don’t know already.

To access the git repo please have a look at the following link.

https://github.com/serhatozdursun/payload_and_responses

--

--

Serhat Ozdursun
Serhat Ozdursun

Written by Serhat Ozdursun

QA Automation Engineer at Unemployment

Responses (1)