I am learning to use API and I have been given the specification of the URL, header and body of an API. The example is shown as follow
url: api/getRate
hearder:{
"msgNo": "abc123",
"receiver": "Customer_A"
}
body:{
"dateTime": "2023/08/19 18:00:00",
"Currency": "USD",
"Ratetype": "COST"
}
How should I use C# method PostAsJsonAsync
to get the result by this API?
Currently, I have the following code. Please tell me whether it is a correct way to use PostAsJsonAsync
in the aforementioned situation.
var posturl = "api/getRate";
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("msgNo", "abc123");
httpClient.DefaultRequestHeaders.Add("receiver", "Customer_A");
var requestBody = new {
"dateTime": "2023/08/19 18:00:00",
"Currency": "USD",
"Ratetype": "COST"};
var response = httpClient.PostAsJsonAsync(posturi, requestBody).Result;