0

Our client is failing on one of our stations and is having issues connecting, so I look up the code and I see this:

var request = new HttpRequestMessage(HttpMethod.Post, "rpcapi")
{
    Content = new StringContent(json, Encoding.UTF8, "application/json")
};
    
var options = isFileDownload 
    ? HttpCompletionOption.ResponseHeadersRead 
    : HttpCompletionOption.ResponseContentRead;
return await _client.SendAsync(request, options, cancellationToken);

Now what confuses me is this line var request = new HttpRequestMessage(HttpMethod.Post, "rpcapi") because I know the address to our backend is not just "rpcapi", but a rather long address.

So how does this work (mind you the same code is working on other stations)? Does the program somehow know what to switch that "rpcapi" for and if so how and where can I find that code?

lee-m
  • 2,269
  • 17
  • 29
Mr.Gomer
  • 501
  • 5
  • 14
  • 1
    How do you create `_client`, and what data type is it? Do you set it's base address somewhere? – gunr2171 Aug 16 '22 at 12:17
  • it is auto injected from what I se: ' public WebApiClient(HttpClient httpClient, string apiKey) – Mr.Gomer Aug 16 '22 at 12:22
  • Then how do you register the HttpClient in DI? – gunr2171 Aug 16 '22 at 12:24
  • i dont even know where to find that the client is in a "Common" aka shared folder and the code base is huge. Let me look around – Mr.Gomer Aug 16 '22 at 12:28
  • You were right there was a base address added to the HttpClient is the rpcapi just added to that base address when you insert it like that. like does it auto concatenate? – Mr.Gomer Aug 16 '22 at 12:43
  • 1
    Does this answer your question? [What is the purpose of HttpClient.BaseAddress and why can't I change it after the first request](https://stackoverflow.com/questions/51319777/what-is-the-purpose-of-httpclient-baseaddress-and-why-cant-i-change-it-after-th) – gunr2171 Aug 16 '22 at 12:51
  • It kinda does. I have found that the client has a BaseAddress attribute set from a Json file so im guessing the rpcapi is just added to that address. – Mr.Gomer Aug 16 '22 at 12:59
  • I found this on MS documentation: When sending a HttpRequestMessage with a relative Uri, the message Uri will be added to the BaseAddress property to create an absolute Uri. – Mr.Gomer Aug 16 '22 at 13:03

1 Answers1

1

My best guess is that "rpcapi" is an alias for the rest of your path. Since I didn't know it was possible to alias a path before helping to find an answer for your question, I think your (backend)application knows what your path will be. You most certainly will have a basepath defined somewhere in your application, which might help you find an answer to your question. This should be somewhere in your api project.

This link might help: What's the difference between HttpRequest.Path and HttpRequest.PathBase in ASP.NET Core?