0

So I’m trying to use wiremock to make a post request to a Url with a request body and then get a response back with my respons.json file.(custom response )

This is how I have it set but I can’t seem to find any info on how to set a post stub with request body(containing data let’s say trying to create ID) and then retrieve a custom response body(created Id response from the json file). How would I set that up. I’m trying to make a request to make id through a json file and then trying to retrieve that created Id .

public void exactUrlOnly() {
stubFor(post(urlEqualTo("/some/thing"))
        .willReturn(aResponse()
      .withHeader("Content-Type", "text/plain")
       .withBodyFile("Hello /world!")));

The WithBodyFile is that the response that’s suppose to be returned ? or is that the request body? I want to somehow put my request body before I start the willReturn line. Basically send a request in with a json file and then get the response from another json file

Samian
  • 11
  • 1
  • 4

1 Answers1

0

Two parts to this:

Matching based on request body.

If you want to stub the request based on a very specific value in the request body, you have use RequestMatching. You can load in the file and then pass it in as needed.

 post(urlEqualTo("/some/thing")).withRequestBody(equalToJson("{}"))

https://wiremock.org/docs/request-matching/

Stubbing Reponse Body

As you have in the post, you can use withBodyFile to specify the file that would be returned. So this is the response body and not the request body.

Just note that the files have to be under a __files directory.

https://wiremock.org/docs/stubbing/ (Specifying the response body section)

https://wiremock.org/docs/response-templating/

Tim
  • 561
  • 2
  • 13
  • My request body is pretty long . Using equalToJson means I have to manually provide the body in there . Is there a way I can throw the json file in inside withRequestBody, just like how i did for the with body file – Samian Aug 19 '22 at 17:42
  • What you could do is to load in the contents from the file first, assign it to a variable and then use that variable in the `equalToJson` method. – Tim Aug 19 '22 at 18:14
  • I tried with byte and tried to read the json file with it. It doesn’t work for some reason. You have any approach for that – Samian Aug 21 '22 at 06:05
  • Use a String rather than byte array. – Tim Aug 22 '22 at 13:19
  • Thanks worked, would you know how to set up multiple response for one request? for instance i make a post request to an end point with the stub and there should be a DRYRUN response initially and then A real response – Samian Aug 22 '22 at 16:09
  • It's deviating from the initial question, but as for multiple responses, depends on what you need. If you have the same response body for every request made, then just need to stub as before. If you need to have different responses using same (but not limited to) request details, then you can look into [**scenarios**](https://wiremock.org/docs/stateful-behaviour/) – Tim Aug 22 '22 at 17:22
  • But with scenarios , I have to make multiple stub for multiple response . The response body is different . The first response body will be a dry run response with all the details and the 2nd response would be the actual response which is created ID – Samian Aug 23 '22 at 00:42
  • I wane just have one post request and get back 2 diff response . One for dry run and one actual . Diff datas kn these – Samian Aug 23 '22 at 00:42
  • Why do you need a dryrun response? Is that what the service would actually behave in real life? – Tim Aug 23 '22 at 08:05
  • Uh I just realized I missed an important fact . It should print out dry run response if there’s a header with dry run if not it should return actual response – Samian Aug 23 '22 at 14:58