0

This is the POST Request, it works.

curl -X POST --header 'Accept: application/json' 

'http://mywebsite.com/api/CompleteService?refId=12345&productPrice=600'

How to send this request using C# => HttpClient => client.PostAsync() method? Two methods have been tried but both failed.


Method 1 (Failed, Response 404)

string url = "http://mywebsite.com/api/CompleteService";
string refId = "12345";
string price= "600";
string param = $"refId ={refId}&productPrice={price}";
HttpContent content = new StringContent(param, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(url, content).Result;

This would only work if API is to accept a request body, not query strings.


Method 2 (Failed, Response 404) https://stackoverflow.com/a/37443799/7768490

var parameters = new Dictionary<string, string> { { "refId ", refId }, { "productPrice", price} };
var encodedContent = new FormUrlEncodedContent(parameters);
HttpResponseMessage response = client.PostAsync(url, encodedContent)
3
  • maybe your end-point has a problem. Commented Sep 18, 2021 at 0:43
  • nope, it's tested working on Swagger, Postman, Axios. Commented Sep 18, 2021 at 0:44
  • Since 404, you have to post your controller and actions headers Commented Sep 18, 2021 at 0:53

1 Answer 1

3

Apparently, None of the tried approaches put parameters in the URL!. You are posting on the below URL

"http://mywebsite.com/api/CompleteService"

However, something that works

string url = "http://mywebsite.com/api/CompleteService?";
string refId = "12345";
string price= "600";
string param = $"refId={refId}&productPrice={price}";
HttpContent content = new StringContent(param, Encoding.UTF8, "application/json");

Attach Parameters to URL and to get Response's Body, Use:

  try   
  {
     HttpResponseMessage response = await client.PostAsync(url + param, content);
     response.EnsureSuccessStatusCode();
     string responseBody = await response.Content.ReadAsStringAsync();
     Console.WriteLine(responseBody);
  }
  catch(HttpRequestException e)
  {
     Console.WriteLine("Message :{0} ",e.Message);
  }
Sign up to request clarification or add additional context in comments.

19 Comments

You probably don't want the space in refId =
@RowanSmith I reused OP codes, and edition welcome.
Thanks for the effort, but nope this did not work. Same result. I cannot post my production API URL here, but I don't think this is a formatting issue.
@JayPow, it's recommended if your body is null, change the end-point to accept GET requests
@JayPow Depending on the context of your app, not awaiting your async call could cause big problems. If you're going to make async calls, make your method async. If you don't need asynchrony, make a synchronous call (Send).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.