How to set request body as json in a mocked put request

120 views
Skip to first unread message

Giorgio Corito

unread,
Jul 5, 2022, 2:45:52 PM7/5/22
to wiremock-user
Hi everyone
I have to mock a rest request (PUT) where the body content is a json content

stubFor(
  put(
    urlPathMatching(url_match_path)).
    withRequestBody(my_json_content).
    willReturn(
      aResponse().
      withStatus(200).
      withHeader("Content-Type", "application/json").
      withBodyFile(xml_path)
)));

I think the red highlighted is not the right solution.

Could you help me, please, for findign the right code for my target.

Thanks in advance to all for your support.

Best regards.

Giorgio

Stephan Bast

unread,
Jul 5, 2022, 4:11:44 PM7/5/22
to Giorgio Corito, wiremock-user
.withBody or .withBodyFile

Holen Sie sich Outlook für Android

From: wiremo...@googlegroups.com <wiremo...@googlegroups.com> on behalf of Giorgio Corito <gioc...@gmail.com>
Sent: Tuesday, July 5, 2022 8:45:51 PM
To: wiremock-user <wiremo...@googlegroups.com>
Subject: How to set request body as json in a mocked put request
 
--
You received this message because you are subscribed to the Google Groups "wiremock-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wiremock-use...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/wiremock-user/55e31f96-e458-4e9a-a35b-1e43a6313b2an%40googlegroups.com.

Stephan Bast

unread,
Jul 5, 2022, 4:14:01 PM7/5/22
to Giorgio Corito, wiremock-user
Oh sorry missread your question. Thought it is the response body

Holen Sie sich Outlook für Android

From: Stephan Bast <stepha...@googlemail.com>
Sent: Tuesday, July 5, 2022 10:11:24 PM
To: Giorgio Corito <gioc...@gmail.com>; wiremock-user <wiremo...@googlegroups.com>
Subject: Re: How to set request body as json in a mocked put request
 

Giorgio Corito

unread,
Jul 6, 2022, 5:18:15 AM7/6/22
to wiremock-user
Thanks Stephan.

G

aaron...@willowtreeapps.com

unread,
Jul 6, 2022, 9:06:03 AM7/6/22
to wiremock-user
You need to provide a matcher for the `withRequestBody` method. Most likely, this will be `equalToJson` -> `.withRequestBody(equalToJson(my_json_content))`.

Check out the examples for `withRequestBody` from these documents for more ways you can match on the request body. 

Reply all
Reply to author
Forward
0 new messages