Offer an .extract() instead of .check() when needing to save optional data

Right now on an http() action you can add .check() to validate if the response has specific data (e.g. status, headers, response body). There is a number of cases where you need to extract data from the response but don’t want to “check” it. It would be more intuitive if there was a .extract() available to do just that.

Here is how we currently have to code it:

http("Search").post("/event/search").body("""{"something":"value"}""").asJSON.check(status.is(200)).check(jsonPath("$.events[*].id").find.dontValidate.saveAs("id"))

There are cases where the response is 200 OK but there is no events returned as the search returned no result. For the longest time I didn’t realize I could add a .dontValidate to the find so that it doesn’t error. If I didn’t have the .dontValidate then I would get this in the logs:

Request 'Search' failed: jsonPath($.events[*].id).exists, found nothing

I was looking around in the docs for something where I could “extract” data. What would be best is something like:

http("Search").post("/event/search").body("""{"something":"value"}""").asJSON.check(status.is(200)).extract(jsonPath("$.events[*].id").find, "id")

Does this seem like a reasonable ask? Is there any other way to do it so that I don’t have to “check” the value?

Hi Shawn,

Honestly, I’m not in favor of introducing several ways of doing the same thing.
Also, best effort extraction is not the majority.

What I agree is that dontValidate is not a very good/intuitive name.
We’ve search for a proper name for a long time, and we failed to realize “optional” was perfect.
We will introduce “optional” in RC3 and deprecate “dontValidate” (will be removed in 2.1).

Cheers,

Stéphane

I agree having multiple ways of doing the same thing is not intuitive either but in this case I think they are two distinct actions. The check is verifying the state of the response while extract is pulling information out of the response so that it can be used within other parts of the application. If I was to go a bit further for argument sake I could see the all of the cases falling under extract:

http("Search").post("/event/search").body("""{"something":"value"}""").asJSON.extract(status, check(is(200)).extract(jsonPath("$.events[*].id").find, saveAs("id"))

I know this probably isn’t the best syntax as I am not as proficient with Scala as most but hopefully that gets my thought across.