jitendra gupta
jitendra gupta

Reputation: 47

How to Pass the Numerical expression in query Param for Rest API automation

I am trying to automate the open source mathjs api which is having the url as "https://api.mathjs.org/v4/?expr=2%2F3&precision=3" . Below is my code

import java.util.TreeMap;
import org.junit.Test;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
public class Mathjs2 {
@Test
public void getResponseBody() {
    RestAssured.baseURI = "https://api.mathjs.org/v4/";
    RequestSpecification httpRequest = RestAssured.given().relaxedHTTPSValidation();
    TreeMap<String, String> temp = new TreeMap<String, String>();
    temp.put("expr", "2%2F3");
    temp.put("precision", "3");
    httpRequest.queryParams(temp);
    Response response = httpRequest.log().all().get();
    System.out.println(response.getStatusCode());
 }
}


 When I have executed the code Iam getting 400 error code while in postman 
 it is showing 200 code. Below is the console log showing the desired url is mismatch 
  Request method:   GET
  Request URI:  https://api.mathjs.org/v4/?expr=2%252F3&precision=3
   

Required Url - https://api.mathjs.org/v4/?expr=2%2F3&precision=3 Generated Url -https://api.mathjs.org/v4/?expr=2%252F3&precision=3

Don't know why the 52 is coming in query param ?expr=2%2F3 Please help on providing and explaining solution

Upvotes: 0

Views: 162

Answers (1)

lucas-nguyen-17
lucas-nguyen-17

Reputation: 5917

Because rest-assured provides urlencode out of the box. You just need tell rest-assured "no urlencode for this one", by using .urlEncodingEnabled(false)

RequestSpecification httpRequest = RestAssured.given().relaxedHTTPSValidation().urlEncodingEnabled(false);

Result:

enter image description here

Reference: https://github.com/rest-assured/rest-assured/wiki/Usage#url-encoding

Upvotes: 1

Related Questions