Drakron
Drakron

Reputation: 95

How to get the Header from a request in a API with C#

I'm building one REST API to my business and I need to get the values from the header in the request but I don't know how?

I need to create a filter in the header and validate this.

Someting like this: header: filterDate = false/true

public CarrierController(ICarrierAppService appService, ICarrierEAppService EAppService)
        {
            _appService = appService;
            _EAppService = EAppService;
        }

        /// <summary>
        /// Create or update a carrier
        /// </summary>
        /// <param name="messageUniqueId">Message Id from external system </param>
        /// <param name="carrierDto">Carrier to create</param>
        /// <returns>Carrier created or updated</returns>
        [HttpPost]
        [ProducesResponseType (typeof (CarrierDto), 200)]
        [ProducesResponseType (typeof (ErrorResponse), 400)]
        public async Task<IActionResult> Post (bool isBusinessE, string messageUniqueId, [FromBody] CarrierDto carrierDto) {

                carrierDto = await _EAppService.UpsertCarrierAsync (carrierDto);
                return CreateResponseOnPost (carrierDto, _name);

        }

Please help me to level up:'/ I'm only a little noob :'/

Upvotes: 1

Views: 1477

Answers (1)

Andy
Andy

Reputation: 13607

Inside your method named Post, you can get to the headers of the request by using Request.Headers dictionary object. For example: Request.Headers["filterDate"]

Upvotes: 1

Related Questions