WithBody delegate is null #341

Closed
opened 2025-12-29 15:21:22 +01:00 by adam · 5 comments
Owner

Originally created by @ZenwalkerD on GitHub (Mar 18, 2021).

Describe the bug

The Func<object,bool> for WithBody function is null for object parameter. Usage example as shown below.
I am using below code:

 private static void Main(string[] args)
        {
            server = WireMockServer.Start(new WireMockServerSettings()
            {
                Port = 9500,
                Urls = new[] { "http://+:9500/" },
                StartAdminInterface = true
            });
                 server.Given(Request.Create().WithPath("/api/sap")
                .UsingPost()
                .WithBody((xmlData)=> {
                         //xmlData is always null
                         return true;
                  }))
                .RespondWith(Response.Create().WithStatusCode(System.Net.HttpStatusCode.OK));
            Thread.Sleep(Timeout.Infinite);
        }

Postman request sent as shown in below image
image

The path is matching, breakpoint hits the delegate method; but object is always null.

Expected behavior:

the passed XML data from client i.e postman should not be null in wiremock api.

Test to reproduce

  • Start the console app with above code
  • Initiate a postman client request as shown in screenshot.
  • Put a breakpoint in delegate method; it hits upon recieving request
  • xmlData object is always null

Dotnet Core 5.0, Console App. Standalone

    <PackageReference Include="wiremock.net" Version="1.4.6" />
    <PackageReference Include="WireMock.Net.StandAlone" Version="1.4.6" />
Originally created by @ZenwalkerD on GitHub (Mar 18, 2021). ### Describe the bug The Func<object,bool> for WithBody function is null for object parameter. Usage example as shown below. I am using below code: ``` c# private static void Main(string[] args) { server = WireMockServer.Start(new WireMockServerSettings() { Port = 9500, Urls = new[] { "http://+:9500/" }, StartAdminInterface = true }); server.Given(Request.Create().WithPath("/api/sap") .UsingPost() .WithBody((xmlData)=> { //xmlData is always null return true; })) .RespondWith(Response.Create().WithStatusCode(System.Net.HttpStatusCode.OK)); Thread.Sleep(Timeout.Infinite); } ``` Postman request sent as shown in below image ![image](https://user-images.githubusercontent.com/869964/111607202-09282180-87fe-11eb-98fe-e35620dff1b5.png) The path is matching, breakpoint hits the delegate method; but object is always null. ### Expected behavior: the passed XML data from client i.e postman should not be null in wiremock api. ### Test to reproduce - Start the console app with above code - Initiate a postman client request as shown in screenshot. - Put a breakpoint in delegate method; it hits upon recieving request - xmlData object is always null ### Other related info Dotnet Core 5.0, Console App. Standalone ``` <PackageReference Include="wiremock.net" Version="1.4.6" /> <PackageReference Include="WireMock.Net.StandAlone" Version="1.4.6" /> ```
adam added the question label 2025-12-29 15:21:22 +01:00
adam closed this issue 2025-12-29 15:21:22 +01:00
Author
Owner

@StefH commented on GitHub (Mar 18, 2021):

Hello @ZenwalkerD,

If you use .WithBody((xmlData), it's assumed that this is JSON data.

If you want to get your example XML data (which is string), you need this code:
image

@StefH commented on GitHub (Mar 18, 2021): Hello @ZenwalkerD, If you use `.WithBody((xmlData)`, it's assumed that this is JSON data. If you want to get your example XML data (which is string), you need this code: ![image](https://user-images.githubusercontent.com/249938/111617565-91e59400-87e3-11eb-81dd-21de4b321c38.png)
Author
Owner

@StefH commented on GitHub (Mar 19, 2021):

@ZenwalkerD : did this answer help you?

@StefH commented on GitHub (Mar 19, 2021): @ZenwalkerD : did this answer help you?
Author
Owner

@ZenwalkerD commented on GitHub (Mar 19, 2021):

As far as i understand, WithBody((parameter)) should have any data format which is being sent from the client, right?
So weather its XML or Json or something. WithBody should give it to parameter.

Either ways, kindly please add a documentation for this.
Also for my task, i took a different approach via RegexMatcher
Thanks

@ZenwalkerD commented on GitHub (Mar 19, 2021): As far as i understand, WithBody((parameter)) should have any data format which is being sent from the client, right? So weather its XML or Json or something. WithBody should give it to parameter. Either ways, kindly please add a documentation for this. Also for my task, i took a different approach via RegexMatcher Thanks
Author
Owner

@StefH commented on GitHub (Mar 19, 2021):

To add flexibility, I updated to code to also support IBodyData, which contains more info about the body.

https://github.com/WireMock-Net/WireMock.Net/blob/master/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs#L78

A new version containing this fix will be released soon.

@StefH commented on GitHub (Mar 19, 2021): To add flexibility, I updated to code to also support IBodyData, which contains more info about the body. https://github.com/WireMock-Net/WireMock.Net/blob/master/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs#L78 A new version containing this fix will be released soon.
Author
Owner

@ZenwalkerD commented on GitHub (Mar 19, 2021):

Thanks, also documentation would be of much help to newbies like me.
Not only w.r.t to this, but also with XMLmatchers i was struggling alot.

@ZenwalkerD commented on GitHub (Mar 19, 2021): Thanks, also documentation would be of much help to newbies like me. Not only w.r.t to this, but also with XMLmatchers i was struggling alot.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#341