1

I am trying to generate a URI for fetching delta records from ServiceNow API. While executing my program, I create a filter and concatenate the same with the original uri and by using get method of RestAssured, I extract the results. Although the above steps are working for full extract but somehow when I try to generate a URI with filter conditions, I am getting an illegal character error.

I have written the following function, for getting the data from API:-

public static void getURIIncrementalExtract(String tblName, String params) {
        RestAssured.baseURI = null;
        String params = ""; //For stackoverflow
        String displayValueParam = "sysparm_display_value=true&";
        String dateParams = "";
        dateParams = "sysparm_query=col_updated_on>=2017-09-30"; //+     fromDate
        String uri = "https://[servicenowAPIadd].com/api/now/table/" +     tblName + "?" + displayValueParam + params + dateParams;
        System.out.println(uri); //URI generated is working correctly in     postman.
        RestAssured.baseURI = uri;
    }

//The uri generated by above function //https://[servicenowAPIadd].com/api/now/table/[DBtablename]?sysparm_display_value=true&sysparm_query=col_updated_on>=2017-09-30

public static Response getResponseByPath() {
        Response response = null;
        try {
            response = RestAssured.given()
                    .when()
                    .auth()
                    .basic(username, password)
                    .get(RestAssured.baseURI);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("Exception in getResponseByPath:-" + e.toString());
        }
        return response;
    }    

In the main method, I call the getURIIncrementalExtract followed by getResponseByPath.

I am getting an illegal syntax exception whenever I try to add the sysparm_query filters. If I remove the filter i.e. "sysparm_query=col_updated_on>=2017-09-30", it works fine.

I am expecting to generate and use the URI for filtered data as well. I think the way I am generating the URI can be wrong. Can someone share a right approach please. Please let me know if further information is required.

Sunny
  • 89
  • 1
  • 9
  • 1
    You can't have `[` and `]` characters in the hostname ... – Stephen C Oct 07 '19 at 01:11
  • 1
    But I think that the real problem is that some of the characters in the query parameters need to be escaped. If you are going to use string bashing to assemble URLs, you need to *understand* the intricacies of URL / URI syntax. It is better (simpler) to use a library method to do the assembly. For example: https://stackoverflow.com/questions/19538431/is-there-a-right-way-to-build-a-url – Stephen C Oct 07 '19 at 01:18
  • Hey Stephen, Thanks for responding to my query. Apologies for the confusion that my example URI had caused but the square brackets have been used as an replacement to actual string. The actual URI doesn't have "[" or "]". I am looking into the string builder though. Will update if I am able to resolve this. – Sunny Oct 07 '19 at 02:13

1 Answers1

1

You need to enable encoding in RestAssured request and pass parameters using queryParam(String, String) method:

RestAssured.baseURI = "https://servicenowAPIadd.com";
RestAssured
    .given()
    .urlEncodingEnabled(true)               //Encoding should be enabled by default anyway
    .queryParam("sysparm_display_value", "true")
    .queryParam("sysparm_query", "col_updated_on>=2017-09-30")
    .when()
    .get("/api/now/table/" + tableName);

In case you need to generate whole url in another place try using URLEncoder (but you should also disable URL encoding in RestAssured to prevent double encoding):

String dateParams = "sysparm_query=" + URLEncoder.encode("col_updated_on>=2017-09-30", "UTF-8"); //Encode your query params here
String uri = "https://servicenowAPIadd.com/api/now/table?" + dateParams;
RestAssured
    .given()
    .urlEncodingEnabled(false)
    .get(uri);
bhusak
  • 1,320
  • 1
  • 9
  • 19
  • Thanks a lot Bhusak and Stephen. I actually created using string builder(following Stephen's suggestion) and now realizing that bhusak's answer is also really good. – Sunny Oct 07 '19 at 04:17