I’ve run into a little data transformation problem, perhaps someone could point me in the right direction?
I need to call a REST API that returns JSON, extract an item from the result, and save its ID for future reference. Normally, this would be easily done with .check, .jsonPath and .saveAs but there’s a snag: the JSON is preceded by an AngularJS JSON vulnerability protection token )]}’, so it is not actually valid JSON.
Let’s say my API is /api/gizmos/ and it returns the following:
)]}’,
{“gizmos”:[{“id”: “ABC”},{“id”: “XYZ”}]}
I cannot use the following because the response body is not valid JSON:
http(“all gizmos”)
.get("/api/gizmos/")
.headers(my_custom_headers)
.check(jsonPath("$.gizmos[0].id").saveAs(“first_id”))
Gatling will then complain as follows:
all gizmos: KO jsonPath($.gizmos[0].id).find(0).exists failed, could not prepare: Could not parse response into a JSON object: Unable to determine the current character, it is not a string, number, array, or object
The current character read is ‘)’ with an int value of 41
Unable to determine the current character, it is not a string, number, array, or object
line number 1
index number 0
)]}’,
^
I’ve tried adding a .transformResponse to delete the token before doing the check, but I get the same error:
http(“all gizmos”)
.get("/api/gizmos/")
.headers(my_custom_headers)
.transformResponse {
case response if response.isReceived =>
new ResponseWrapper(response) {
override val body = StringResponseBody(response.body.string.replace(")]}’,", “”), UTF_8)
}
}
.check(jsonPath("$.gizmos[0].id").saveAs(“first_id”))
I cannot do .check(bodyString.transform(…) because jsonPath cannot be used then (it can only be used as a starting point)
I’m using Gatling 2.1.5. What am I doing wrong?