Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
[HttpPut]
public async Task<IActionResult> CreatePerson([FromBody]CreatePersonCommand command, [FromHeader(Name = "x-requestid")] string requestId)

This is a web api method, which is accessed by another project.

The client code looks like this:

var response = await _httpClient.PutAsync(uri, personContent);

Is it possible to access the x-requestid at the client in the response that is sent from the web api project?

In case you are trying to get it from response, the answer would be yes, but only if the web api populates it in the response. I assume that since the web api tries to bind to it that it was included in the request. It will only be in the response if the web pi adds it. – Nkosi Apr 17, 2019 at 13:29

In case you need to get value:

var response = await _httpClient.PutAsync(uri, personContent);
response.Headers.TryGetValues("x-requestid", out var headerValues);

If you need to add headers to the response in your Web API, you have access to Response entity in your controller which inherits from ASP ControllerBase class:

Response.Headers.Add("x-requestid", "value");
                How would I add x-requested to the response from the web api? I added it to the request from the MVC client by creating a HttpClientRequestIdDelegatingHandler class.  Thanks.
– w0051977
                Apr 17, 2019 at 13:47
                How do you add the header to the response in the web api.  I assume that I have to create a class, which extends something?
– w0051977
                Apr 17, 2019 at 13:54
                @w0051977 check for the update again. Sorry your question was a bit unclear from the beginning
– OlegI
                Apr 17, 2019 at 13:58
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.