Rest with parameter in request header


Please see attached screenshots. I am trying to make a Rest request with a parameterised Header Value.

Is this possible? It appears that this should work but when I add the parameter the GET returns no records. If I simply enter the correct value (without parametrising) it returns the expected data.

I have just noticed that the second screenshot states that the Parameter is in a Query (this is an error). I have actually had the setting at Header and this failed.

Thanks for your help.

Regards

Peter


Hi @PeterS,

Can you post the generated code for this method in the service class?

Vladimar

Please find below code in StaffologyDataHttpHandler.cs and StaffologyDataService.cs. To get around this problem I have set up a static class to hold the apiKey so it is no longer critical for me.

However I will be grateful to understand a more correct way of achieving this if possible.

Thanks for your help.

Regards

Peter

using System;
using System.Web;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Threading;

namespace Staffology
{
public partial class StaffologyDataHttpHandler : DelegatingHandler
{
public StaffologyDataHttpHandler()
{
this.InnerHandler = new HttpClientHandler();
}

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        request.Headers.Add("Authorization", "{apiKey}");

        return await base.SendAsync(request, cancellationToken);
    }
}

}

using System;
using System.Collections.Generic;
using System.Web;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Staffology.Models.StaffologyData;

namespace Staffology
{
public partial class StaffologyDataService
{
private readonly HttpClient httpClient;
private readonly Uri baseUri;

    public StaffologyDataService(StaffologyDataHttpHandler httpHandler, IConfiguration configuration)
    {
        this.httpClient = new HttpClient(httpHandler);
        this.baseUri = new Uri(configuration["StaffologyDataBaseUri"]);
    }

    partial void OnGetEmployers(HttpRequestMessage requestMessage);
    partial void OnGetEmployersResponse(HttpResponseMessage responseMessage);

    public async Task<IEnumerable<Employers>> GetEmployers(string getApi)
    {
        var uri = new Uri(baseUri, $"employers");

        var message = new HttpRequestMessage(HttpMethod.Get, uri);

        message.Headers.Add("getApi", getApi);

        OnGetEmployers(message);

        var response = await httpClient.SendAsync(message);

        response.EnsureSuccessStatusCode();

        OnGetEmployersResponse(response);

        using (var stream = await response.Content.ReadAsStreamAsync())
        {
            return await JsonSerializer.DeserializeAsync<IEnumerable<Employers>>(stream, new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                PropertyNameCaseInsensitive = true,
            });
        }
    }
}

}