Amything 122 Posted December 14, 2024 Posted December 14, 2024 (edited) Hey, I have the code below which if I use with GET it works fine and I get the Authorization header . However if using POST the header does not get delivered. I don't see any hints in the log: 2024-12-13 23:41:33.779 Info MyPlugin: Sending POST request to http://192.168.50.205:5016/api/report 2024-12-13 23:41:33.779 Info HttpClient: POST http://192.168.50.205:5016/api/report I've verified with Postman that my server works as expected. Does anyone have a POST example I could check out or is it a known problem perhaps? private async Task<string> WebRequestAPI(string url, CancellationToken cancellationToken, string method, string postData = null, string apiKey = null) { if (string.IsNullOrEmpty(apiKey)) { logging.Error($"apiKey is null or empty, cannot make request to {url}"); return null; } logging.Info($"Sending {method} request to {url}"); var options = new HttpRequestOptions { CancellationToken = cancellationToken, Url = url, TimeoutMs = 20000, }; options.RequestHeaders.Add("Authorization", $"Bearer {apiKey}"); HttpResponseInfo response; try { if (method.ToUpper() == "POST") { if (string.IsNullOrEmpty(postData)) { logging.Error($"postData is null or empty, cannot make POST request to {url}"); return null; } options.RequestContent = postData.AsMemory(); options.RequestContentType = "application/json"; response = await _httpClient.Post(options).ConfigureAwait(false); using (var stream = response.Content) using (var reader = new StreamReader(stream)) { var result = await reader.ReadToEndAsync().ConfigureAwait(false); logging.Info($"Received response: {result}"); return result; } } else { using (var stream = await _httpClient.Get(options).ConfigureAwait(false)) using (var reader = new StreamReader(stream)) { var result = await reader.ReadToEndAsync().ConfigureAwait(false); logging.Info($"Received response: {result}"); return result; } } } catch (HttpException ex) { logging.ErrorException($"Error from {url} using method {method}", ex); if (ex.IsTimedOut) { logging.ErrorException($"Could not connect to host {url}", ex); } return string.Empty; } } Edited December 14, 2024 by Amything
Amything 122 Posted December 14, 2024 Author Posted December 14, 2024 Forgot to mention that I was testing this on 4.9.0.33 beta.
Luke 42077 Posted December 14, 2024 Posted December 14, 2024 HI, there has to be something else going on. We have our own usages of this that are doing Post with headers and they are working just fine.
Amything 122 Posted December 14, 2024 Author Posted December 14, 2024 Yeah, I'm a bit stumped at the moment. I'll try to make a minimal example and see how it goes.
Amything 122 Posted December 14, 2024 Author Posted December 14, 2024 I tried to post it https://httpbin.org/post which just returns the response and I can see the header. So this is on my end somewhere. 1
Amything 122 Posted December 14, 2024 Author Posted December 14, 2024 If I rename from Authorization to Auth it works. Bunch of people seeing the same issue, it's a server configuration issue. I was just so convinced it was a IHttpClient problem because GET worked and also Postman works. With further testing Postman works only if it's on the same machine. Cheers! 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now