I have the following postman
collection :
{
"info": {
"_postman_id": "b172f7d9-xxxxxxx",
"name": "protection",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "protection-example_IP ONLY",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/x-www-form-urlencoded"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "ApiKey",
"value": "62fdc812-be58-xxxxx",
"type": "text"
},
{
"key": "TagId",
"value": "1111",
"type": "text"
},
{
"key": "ClientIP",
"value": "200.55.111.111",
"type": "text"
},
{
"key": "RequestURL",
"value": "http://awesomeSite.com",
"type": "text"
},
{
"key": "ResourceType",
"value": "text/html",
"type": "text"
},
{
"key": "Method",
"value": "GET",
"type": "text"
},
{
"key": "Host",
"value": "awesomeSite.com",
"type": "text"
},
{
"key": "UserAgent",
"value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
"type": "text"
},
{
"key": "Accept",
"value": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"type": "text"
},
{
"key": "AcceptLanguage",
"value": "en-IL,en-US;q=0.9,en;q=0.8,my;q=0.7",
"type": "text"
},
{
"key": "AcceptEncoding",
"value": "gzip, deflate, br",
"type": "text"
},
{
"key": "HeaderNames",
"value": "Host,User-Agent,Accept,Accept-Langauge,Accept-Encoding,Cookie",
"type": "text"
}
]
},
"url": {
"raw": "https://other.awesome.com/test-api",
"protocol": "https",
"host": [
"other",
"awesome",
"com"
],
"path": [
"test-api"
]
}
},
"response": []
},
//.........
I need to create the same request using rest-assured
framework,
I tried to create a map with keys/value and put it to the body:
Map<String,String> keys = new HashMap<String, String>(){{
put("ApiKey", "62fdc812-be58-xxxxxx");
put("TagId", "1111");
//...
}};
RestAssured.given()
.contentType("application/x-www-form-urlencoded")
.body(keys)
.when().post("https://other.awesome.com/test-api")
.then()
.statusCode(200);
But got the error :
java.lang.IllegalArgumentException: Cannot serialize because cannot determine how to serialize content-type application/x-www-form-urlencoded.
In the case when I use formParams
instead body
:
RestAssured.given()
.log().all()
.formParams(keys)
.when().post("https://other.awesome.com/test-api")
.then()
.statusCode(200);
I get the following error:
Expected status code <200> doesn't match actual status code <415>
HTTP/1.1 415 Unsupported Media Type
Content-Length: 149
Content-Security-Policy: default-src 'none'
Content-Type: text/html; charset=utf-8
Date: Tue, 05 Jan 2021 09:50:12 GMT
X-Content-Type-Options: nosniff
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Error</title>
</head>
<body>
<pre>Unsupported Media Type</pre>
</body>
</html>
How to correctly build request with Rest Assured for this pattern ?
In the PostMan
request contains key/value
map in the body
. Tab Params
is empty. But In the code, in the second case I haven't any data in the body
section:
You are nearly there,
Map :
Map<String, String> keys = new HashMap<String, String>() {
{
put("ApiKey", "62fdc812-be58-xxxxxx");
put("TagId", "1111");
}
};
Rest Assured Code :
RestAssured.given().formParams(keys).when().post("https://other.awesome.com/test-api").then()
.statusCode(200);
Rest Assured sets the Content-Type
as application/x-www-form-urlencoded
automatically when you use the formParams()
Update :
I'm guessing you are getting Unsupported Media Type
because Rest Assured sets a default charset in the request for Content-Type
Try with the below code
RestAssured.given().log().all()
.config(RestAssured.config()
.encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)))
.formParams(keys).when().post("https://other.awesome.com/test-api").then().statusCode(200);
You need to import encoderConfig as static
import static io.restassured.config.EncoderConfig.encoderConfig;
Check this link for more information on the encoderConfig
If you still have issues, From POSTMAN follow the below steps
Add a .log().all()
after given()
in your Rest Assured code and compare the results