wiremock java api - stub request body in form of form-data (not string, nor json)

Giorgos Lamprakis :

As the title says I have to stub a post request with request body in the form of form data. No json, string, or xml. I am using junit and wiremock's java api.

I am doing something like the following:

 wireMockServer.stubFor(post(urlEqualTo(MY_URI))
               .withRequestBody(
          // how do I stub a body in form data format??                  
          ).willReturn(aResponse().withStatus(200).withHeader("content-type", "application/json").withBody(expectedBody))
        );

any ideas of what to put in the code instead of the comment?

Thank you!

Tom :

There isn't a specific form matcher in WireMock right now (there should be and I keep meaning to work on it). However, you can do something like this:

wireMockServer.stubFor(post(MY_URI)
            .withRequestBody(containing("key1=value1"))
            .withRequestBody(containing("key2=value2"))
            .willReturn(okJson(expectedResponse));

Note, you'll need to URL encode value1, value2 etc. if there are any metacharacters in there.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=140407&siteId=1