Dynamic http status code? #576

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

Originally created by @eetawil on GitHub (Feb 26, 2024).

Originally assigned to: @StefH on GitHub.

Hi,
I have this mock setup like this and now I want to also make the status code dynamic (200 or 400) based on the request that I will parse in the "GenerateBody" method, how can I achieve that here?

wireMockServer
                .Given(Request.Create().WithPath("/validatePDFA").UsingPost())
                .RespondWith(Response.Create()                   
                    .WithHeader("Content-Type", "application/json")
                    .WithBody(req => GenerateBody(req))
                    .WithStatusCode(??) 
                    // the "req" returned from the "GenerateBody" could contain the status, but how to retrieve it afterward?
                );
Originally created by @eetawil on GitHub (Feb 26, 2024). Originally assigned to: @StefH on GitHub. Hi, I have this mock setup like this and now I want to also make the status code dynamic (200 or 400) based on the request that I will parse in the "GenerateBody" method, how can I achieve that here? ``` c# wireMockServer .Given(Request.Create().WithPath("/validatePDFA").UsingPost()) .RespondWith(Response.Create() .WithHeader("Content-Type", "application/json") .WithBody(req => GenerateBody(req)) .WithStatusCode(??) // the "req" returned from the "GenerateBody" could contain the status, but how to retrieve it afterward? ); ```
adam added the question label 2025-12-29 15:27:21 +01:00
adam closed this issue 2025-12-29 15:27:21 +01:00
Author
Owner

@StefH commented on GitHub (Mar 2, 2024):

So you are looking for this code?

.WithStatusCode(Func<IRequestMessage, int> statusCodeFactory) 

which can be used like:

.WithStatusCode(req => GenerateStatus(req)) 
@StefH commented on GitHub (Mar 2, 2024): So you are looking for this code? ``` c# .WithStatusCode(Func<IRequestMessage, int> statusCodeFactory) ``` which can be used like: ``` c# .WithStatusCode(req => GenerateStatus(req)) ```
Author
Owner

@eetawil commented on GitHub (Mar 4, 2024):

Yes this could work , but I was wondering if there's a way to do it in a single shot?

 .WithBody(req => GenerateBody(req))
.WithStatusCode(??)  <== Use here the result from GenerateBody

Here when generating the body dynamically can it also set the status code without the need of reprocessing the request again?

Or maybe the Obj returned by GenerateBody => Func<IRequestMessage, Obj> could be retrieved and reused in .WithStatusCode ???

@eetawil commented on GitHub (Mar 4, 2024): Yes this could work , but I was wondering if there's a way to do it in a single shot? ``` .WithBody(req => GenerateBody(req)) .WithStatusCode(??) <== Use here the result from GenerateBody ``` Here when generating the body dynamically can it also set the status code without the need of reprocessing the request again? Or maybe the `Obj `returned by GenerateBody => `Func<IRequestMessage, Obj> ` could be retrieved and reused in `.WithStatusCode` ???
Author
Owner

@StefH commented on GitHub (Mar 4, 2024):

In that case. You need "WithCallback".

@StefH commented on GitHub (Mar 4, 2024): In that case. You need "WithCallback".
Author
Owner

@eetawil commented on GitHub (Mar 5, 2024):

Thank you I didn't know about that, I'll look into that. Again, thank you for answering our questions and being available!

@eetawil commented on GitHub (Mar 5, 2024): Thank you I didn't know about that, I'll look into that. Again, thank you for answering our questions and being available!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#576