'Replicating a Javascript request using Httpclient in a C# API application
I have a web application that needs to send data to a third party, the only current means to do this is via JavaScript on the form. The form itself uses back end validation and completes the rest of the request by handing it off to an API, so I would like to replicate the process within the back end without having to rely on JavaScript.
To this end, I have replicated the Json object and created a HTTP client request, which is receiving a 200 success response, however the data isn't being populated their end.
I feel that I am missing the http2 psudo heading ":authority" - Please correct me if I'm using the wrong terminology.
This is the request I have so far
using (var client = new HttpClient(handler))
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("br"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authority", "my.target.domain.com");
var jsonString = JsonConvert.SerializeObject(config);
var content = new StringContent(jsonString, Encoding.UTF8, "application/json");
var response = client.PostAsync(config.ServiceUrl, content).Result;
ect ect....
}
And this is what Dev tools in Chrome tells me about the request
:authority: my.target.domain.com
:method: POST
:path: /service.url
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: en-GB,en-US;q=0.9,en;q=0.8
content-length: 1356
content-type: application/json; charset=UTF-8
dnt: 1
origin: https://www.example.com
referer: https://www.example.com/
sec-ch-ua: " Not;A Brand";v="99", "Google Chrome";v="97", "Chromium";v="97"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: cross-site
Am I missing something? I can't seem to find any information about sending the "Authority" header, so I have guessed at how to do it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
