WithCallback leads to empty response #222

Closed
opened 2025-12-29 14:25:34 +01:00 by adam · 5 comments
Owner

Originally created by @Mies75 on GitHub (Oct 15, 2019).

When I use .WithBody(@"<soapenv:Envelope etc. I get a response that is can be handled well by our applications.

However when we use a callback, the response message is empty :

.WithCallback(req =>
{
	return CoverApplicationResponseBuilder.CreateResponse(req);
})
public class CoverApplicationResponseBuilder
{
	public static ResponseMessage CreateResponse(RequestMessage requestSoapMessage)
	{
		//get some vars from the requestSoapMessage (excerpt)
		XDocument doc = XDocument.Parse(requestSoapMessage.Body);

		//now replace some vars in the response (also an excerpt)
		var response = new StringBuilder(ResponseMessages.CoverApplicationResponse);

		return new ResponseMessage { BodyData = new WireMock.Util.BodyData { BodyAsString = response.ToString() } };
	}
}

Leads to:

"Response": {
"StatusCode": 200,
"Headers": {},
"BodyDestination": null,
"Body": null,
"BodyAsJson": null,
"BodyAsBytes": null,
"BodyAsFile": null,
"BodyAsFileIsCached": null,
"BodyOriginal": null,
"BodyEncoding": null,
"DetectedBodyType": 0,
"DetectedBodyTypeFromContentType": 0
},

Am I doing something wrong?

Regards,

Michel

Originally created by @Mies75 on GitHub (Oct 15, 2019). When I use `.WithBody(@"<soapenv:Envelope ` etc. I get a response that is can be handled well by our applications. However when we use a callback, the response message is empty : ``` c# .WithCallback(req => { return CoverApplicationResponseBuilder.CreateResponse(req); }) ``` ``` c# public class CoverApplicationResponseBuilder { public static ResponseMessage CreateResponse(RequestMessage requestSoapMessage) { //get some vars from the requestSoapMessage (excerpt) XDocument doc = XDocument.Parse(requestSoapMessage.Body); //now replace some vars in the response (also an excerpt) var response = new StringBuilder(ResponseMessages.CoverApplicationResponse); return new ResponseMessage { BodyData = new WireMock.Util.BodyData { BodyAsString = response.ToString() } }; } } ``` Leads to: ``` js "Response": { "StatusCode": 200, "Headers": {}, "BodyDestination": null, "Body": null, "BodyAsJson": null, "BodyAsBytes": null, "BodyAsFile": null, "BodyAsFileIsCached": null, "BodyOriginal": null, "BodyEncoding": null, "DetectedBodyType": 0, "DetectedBodyTypeFromContentType": 0 }, ``` Am I doing something wrong? Regards, Michel
adam added the question label 2025-12-29 14:25:34 +01:00
adam closed this issue 2025-12-29 14:25:34 +01:00
Author
Owner

@StefH commented on GitHub (Oct 15, 2019):

You need to provide DetectedBodyType = BodyType.String

See
https://github.com/WireMock-Net/WireMock.Net/blob/master/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithCallbackTests.cs#L21

@StefH commented on GitHub (Oct 15, 2019): You need to provide `DetectedBodyType = BodyType.String` See https://github.com/WireMock-Net/WireMock.Net/blob/master/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithCallbackTests.cs#L21
Author
Owner

@Mies75 commented on GitHub (Oct 16, 2019):

Great, that works!
I also had to add:
responseMsg.Headers.Add("Content-Type", new WireMock.Util.WireMockList<string>() { "text/xml; charset=utf-8" } );

It seems that one has to set all message properties in the callback. Would it be possible to still take the headers and status code from .RespondWith instead of arranging it in the callback?

@Mies75 commented on GitHub (Oct 16, 2019): Great, that works! I also had to add: `responseMsg.Headers.Add("Content-Type", new WireMock.Util.WireMockList<string>() { "text/xml; charset=utf-8" } );` It seems that one has to set all message properties in the callback. Would it be possible to still take the headers and status code from `.RespondWith` instead of arranging it in the callback?
Author
Owner

@StefH commented on GitHub (Oct 16, 2019):

If you only want to manipulate the body, then you can use:

WithBody([NotNull] Func<RequestMessage, string> bodyFactory, .....

See example line 193 in 93682c9bbf/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithBodyTests.cs (L189-L193)

@StefH commented on GitHub (Oct 16, 2019): If you only want to manipulate the body, then you can use: `WithBody([NotNull] Func<RequestMessage, string> bodyFactory, .....` See example line 193 in https://github.com/WireMock-Net/WireMock.Net/blob/93682c9bbffa9a1e18dac6fa06c31d396eafb9bd/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithBodyTests.cs#L189-L193
Author
Owner

@Mies75 commented on GitHub (Oct 16, 2019):

Cool, that works. I rather have my transform functions return strings instead of ResponseMessages.
Thanks!

@Mies75 commented on GitHub (Oct 16, 2019): Cool, that works. I rather have my transform functions return strings instead of `ResponseMessages`. Thanks!
Author
Owner

@StefH commented on GitHub (Oct 16, 2019):

OK. Good to hear.

Closing...

@StefH commented on GitHub (Oct 16, 2019): OK. Good to hear. Closing...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#222