Is it possible to access custom headers inside callback? #495

Closed
opened 2025-12-29 08:29:05 +01:00 by adam · 4 comments
Owner

Originally created by @jamiemcl on GitHub (Feb 28, 2023).

Originally assigned to: @StefH on GitHub.

Hi,

I have a scenario where I need to match based on a specific header, and then retrieve the value of this header inside the WithCallback method. I can get this mock to match, however, when I try to access the value from request message headers, the header does not exist and I therefore cannot retrieve the value.

.Given(Request.Create()
                    .WithPath("/random-path")
                    .WithHeader("x-test-id", "*")
                    .UsingGet()
                )
                .RespondWith(Response.Create()
                    .WithCallback(requestMessage =>
                    {
                        // ALWAYS RETURNS FALSE
                        if (requestMessage.Headers is not null && requestMessage.Headers.TryGetValue("x-test-id", out var testId))

Is there anything I can do to retrieve the value of the x-test-id header here?

Originally created by @jamiemcl on GitHub (Feb 28, 2023). Originally assigned to: @StefH on GitHub. Hi, I have a scenario where I need to match based on a specific header, and then retrieve the value of this header inside the WithCallback method. I can get this mock to match, however, when I try to access the value from request message headers, the header does not exist and I therefore cannot retrieve the value. ``` c# .Given(Request.Create() .WithPath("/random-path") .WithHeader("x-test-id", "*") .UsingGet() ) .RespondWith(Response.Create() .WithCallback(requestMessage => { // ALWAYS RETURNS FALSE if (requestMessage.Headers is not null && requestMessage.Headers.TryGetValue("x-test-id", out var testId)) ``` Is there anything I can do to retrieve the value of the x-test-id header here?
adam added the question label 2025-12-29 08:29:05 +01:00
adam closed this issue 2025-12-29 08:29:05 +01:00
Author
Owner

@StefH commented on GitHub (Feb 28, 2023):

Are you sure the request you send to WireMock.Net contains the x-test-id header?

@StefH commented on GitHub (Feb 28, 2023): Are you sure the request you send to WireMock.Net contains the `x-test-id` header?
Author
Owner

@jamiemcl commented on GitHub (Feb 28, 2023):

Thanks so much for the rapid reply!

Yes, I am sure that the request containers the header. As I mentioned, the issue isn’t with the mock not matching as the callback does get run with the above setup, but there are only 4 headers present on the requestMessage - none of which are my custom header but instead include Host and 3 others.

Any further thoughts?

@jamiemcl commented on GitHub (Feb 28, 2023): Thanks so much for the rapid reply! Yes, I am sure that the request containers the header. As I mentioned, the issue isn’t with the mock not matching as the callback does get run with the above setup, but there are only 4 headers present on the requestMessage - none of which are my custom header but instead include Host and 3 others. Any further thoughts?
Author
Owner

@StefH commented on GitHub (Feb 28, 2023):

With this C# code:

server
    .Given(Request.Create().WithPath("/random200or505").UsingGet())
    .RespondWith(Response.Create().WithCallback(request =>
    {
        int code = new Random().Next(1, 2) == 1 ? 505 : 200;
        return new ResponseMessage
        {
            BodyData = new BodyData
            {
                BodyAsString = "random200or505:" + code + ", HeadersFromRequest = " + string.Join(",", request.Headers),
                DetectedBodyType = Types.BodyType.String,
            },
            StatusCode = code
        };
    }));

And sending a GET to /random200or505

I get all headers back:
image

@StefH commented on GitHub (Feb 28, 2023): With this C# code: ``` c# server .Given(Request.Create().WithPath("/random200or505").UsingGet()) .RespondWith(Response.Create().WithCallback(request => { int code = new Random().Next(1, 2) == 1 ? 505 : 200; return new ResponseMessage { BodyData = new BodyData { BodyAsString = "random200or505:" + code + ", HeadersFromRequest = " + string.Join(",", request.Headers), DetectedBodyType = Types.BodyType.String, }, StatusCode = code }; })); ``` And sending a GET to /random200or505 I get all headers back: ![image](https://user-images.githubusercontent.com/249938/221956890-0ff7200b-c074-42ce-b8d3-8f58241e7468.png)
Author
Owner

@jamiemcl commented on GitHub (Mar 1, 2023):

You are right, this is my mistake. I was adding the header to the initial request, which then subsequently made a second request inside of it. Sorry to waste your time, and thanks again for the prompt replies

@jamiemcl commented on GitHub (Mar 1, 2023): You are right, this is my mistake. I was adding the header to the initial request, which then subsequently made a second request inside of it. Sorry to waste your time, and thanks again for the prompt replies
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#495