Need to know a simple way to add a http header for already existing http request.
Below is my middle ware code
public class ProviderStateMiddleware
{
private ITestOutputHelper _outputHelper { get; }
private const string ConsumerName = "test";
private readonly RequestDelegate _next;
private readonly IDictionary<string, Action> _providerStates;
public ProviderStateMiddleware(RequestDelegate next)
{
_next = next;
_providerStates = new Dictionary<string, Action>
{
{
"A session id",
getSessionID
},
};
}
private void getSessionID()
{
}
public async Task Invoke(HttpContext context)
{
if (context.Request.Path.Value == "/provider-states")
{
this.HandleProviderStatesRequest(context);
await context.Response.WriteAsync(String.Empty);
}
else
{
await this._next(context);
}
}
private void HandleProviderStatesRequest(HttpContext context)
{
context.Response.StatusCode = (int)HttpStatusCode.OK;
if (context.Request.Method.ToUpper() ==
HttpMethod.Post.ToString().ToUpper() &&
context.Request.Body != null)
{
string jsonRequestBody = String.Empty;
using (var reader = new StreamReader(context.Request.Body,
Encoding.UTF8))
{
jsonRequestBody = reader.ReadToEnd();
}
var providerState = JsonConvert.DeserializeObject<ProviderState>
(jsonRequestBody);
//A null or empty provider state key must be handled
if (providerState != null &&
!String.IsNullOrEmpty(providerState.State) &&
providerState.Consumer == ConsumerName)
{
_providerStates[providerState.State].Invoke();
}
}
}
}
}
I am new to c# or the http middleware part, please let me know as to where its feasible to add a custom header, like below json. I read some posts here, but was not much to my understanding.
{Subscriber-id : "1234"}
This was handled with PactVerifierConfig.CustomHeader method, which adds request headers.
var config = new PactVerifierConfig
{
Outputters = new List<IOutput>
{
new XUnitOutput(_outputHelper)
},
//Custom header
CustomHeader = new KeyValuePair<string, string>("testId","test123"),
// Output verbose verification logs to the test output
Verbose = true
};