Invalid request is being made when sent with variable

I am unable to POST the request for the API. Any thoughts, how can this be solved please ?

Here is the script -
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testobject.TestObject

“Send the POST Request”
response = WS.sendRequest(findTestObject(‘Object Repository/POST’, [(‘url’) : url]))

“Capture the response code and verify”
println response.getResponseText()
println response.getHeaderFields()
println response.getResponseBodySize()
println response.getStatusCode()
WS.verifyResponseStatusCode(response, 200)
WS.verifyElementPropertyValue(response, “status”, 200)
WS.verifyElementPropertyValue(response, “url”, “google.com”)
WS.verifyElementPropertyValue(response, ‘queries.getinfo.cats[0].catid’, 50)

Here is the error -

2019-01-22 14:20:41.379 INFO mc.k.katalon.core.main.TestCaseExecutor --------------------
2019-01-22 14:20:41.385 INFO mc.k.katalon.core.main.TestCaseExecutor START Test Cases/BCTI/UrlGetInfoModified
2019-01-22 14:20:42.603 INFO mc.k.katalon.core.main.TestCaseExecutor (Default) url = google.com
2019-01-22 14:20:45.333 DEBUG mtestcase.UrlGetInfoModified response = sendRequest(findTestObject(“Object Repository/POST”, [“url”:url]))
2019-01-22 14:20:49.665 DEBUG mtestcase.UrlGetInfoModified 2: println(response.getResponseText())
{
“status”: 403,
“status_description”: “InvalidRequest”,
“error_code”: 1007,
“error_code_description”: “InvalidRequestFormat”,
“errormsg”: “ISIS.InvalidRequestException: Unexpected character encountered while parsing value: g. Path ‘urls’, line 1, position 110.
—> Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: g. Path ‘urls’, line 1, position 110.\r\n
at Newtonsoft.Json.JsonTextReader.ParseValue()\r\n
at Newtonsoft.Json.JsonTextReader.ReadInternal()\r\n
at Newtonsoft.Json.JsonTextReader.Read()\r\n
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)\r\n
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)\r\n
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)\r\n
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)\r\n
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)\r\n
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)\r\n
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)\r\n
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)\r\n
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)\r\n
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)\r\n
at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)\r\n
at ISIS.ApiRequest.ParseJsonRequest(String strVersion, Noun noun, String strJson) in C:\Users\hho\Works\WR_Projects\ISIS\Core\ApiRequestProcessing\ApiRequest.cs:line 1329\r\n
— End of inner exception stack trace —\r\n
at ISIS.ApiRequest.ParseJsonRequest(String strVersion, Noun noun, String strJson) in C:\Users\hho\Works\WR_Projects\ISIS\Core\ApiRequestProcessing\ApiRequest.cs:line 1430\r\n
at ISIS.ApiRequest.CreateRequest(String strVersion, Noun noun, String strRequestBody, String clientIp) in C:\Users\hho\Works\WR_Projects\ISIS\Core\ApiRequestProcessing\ApiRequest.cs:line 98\r\n
at ISIS.ApiHandler.d__0.MoveNext() in C:\Users\hho\Works\WR_Projects\ISIS\Core\RestApi\ApiHandler.cs:line 85”
}

@spullabhotla, have you tried sending the request with the url defined explicitly, not via variable? Does the error still occur in that case?

Yes, the manual way of sending the request works good. I do not see any problem with it. However, parameterize it, the request send breaks.

@spullabhotla, can you send us your request object file, so that we have more clues to investigate the issue and help you better?

@huynguyen - are you asking about the .rs file ?

Yes, @spullabhotla

I am unable to attach the file. Seems to be the attachments only accept the picture/graphic files. So I have tried changing it to .png but because of the size is too large for it to attach, the attachments option rejected it.

Any thoughts, how should I share it ?

@spullabhotla, can you zip the file and attach it? We have already supported attaching zip file.

Please find the zip. It was zipped using 7Zip. If the unzip doesn’t work, please change the extension to 7z.

Appreciate your help.

Thanks, @spullabhotla. We will investigate the issue and get you noticed ASAP.

Thank you Huy. Appreciate your time.

@huynguyen Any update on this, please ?

@spullabhotla, you can put your url variable definition in quotes, i.e “urls”: [${url}] to “urls”: ["${url}"]. This is because the quotation marks for string value will not be included on variable value substitution.

@huynguyen - thank you very much. It worked. I was able to successfully post the request. Now, I will start the data driven test to work on it with some data. Our aim to rum some data starting with 100s, then thousands, hundred thousands and millions. I hope the tool will definitely withstand the loads of data provided in CSV file.
I will give it a shot and let you know.

1 Like

Thank you and sorry, I had to delete the .rs attachment for security purposes. Appreciate your help.

@spullabhotla @huynguyen
How did it worked for you?
I am facing a similar challenge.

  1. I have a RestAPI which has a numeric ID in it
  2. I want to run the request in loop for multiple IDs, importing data from excel
  3. I am able to import the data from excel but for some reasons the request URL is not accepting the ID from excel file, thereby giving a bad response in return

Here is my code for the process, any help will be appreciated

TestData data = findTestData('BookIDs/book_ids')
for (int j = 1; j <= 21; j++) {
String value = data.getValue(1, j)
def response = WS.sendRequest(findTestObject('JG_APIs/BookDetails', [('BookID') : value]))

WS.verifyResponseStatusCode(response, 200,FailureHandling.CONTINUE_ON_FAILURE)

println(value)

}’

The println is used to verify if i am fetching correct data from excel and as I checked in Console, it was correct.

@Bal_Mukund,
Can you send us your request file (.rs file) together with the data file? We will have more clues to investigate the issue and help you better.

@huynguyen Yes, here is the URL, includes the data file and rs file.

@Bal_Mukund
Mine was an easy fix. I have changed from “urls”: [${url}] to “urls”: [“${url}”] and it worked like charm.
huynguyen helped me with that and I truly appreciate his help. Our team is very happy to learn it and implement it.

1 Like

Thank @Bal_Mukund,
I have investigated your issue and found out that there are empty spaces in the request params of your URL. This is undesired result as Katalon is expected to handle escaping these spaces automatically. We will improve on this. In the mean time, please try removing these spaces and test again.