0

Does anyone know how to pass in Body (raw) of an API using C# code? I am testing my API using Postman. For this particular url, I will need to insert a Body to get an output. I am not sure how do I pass in the Body using C# code.

This is my code in C# to retrieve the data from the url, it works perfectly fine.

  var httpClient = new HttpClient();
  var response = await httpClient.GetStringAsync(url);

This is just an example. With my own url and body, when I click Send, there will be an output shown. So how do I pass in the body text in C#?

Charis
  • 111
  • 3
  • 11
  • Are you asking how to **set an outgoing request's body** (using `HttpClient`), or how to **get an incoming request's body** (in ASP.NET Web API) - or how to **get the response body** (in `HttpClient`) - or how to **set a raw response body** (in ASP.NET Web API)? – Dai Apr 06 '20 at 02:12
  • Your screenshot and code shows a GET request, but a GET request cannot have a request body, which makes this question even more confusing. Only POST, PUT, and PATCH requests can have request-bodies. – Dai Apr 06 '20 at 02:13
  • @Dai Some APIs, such as ElasticSearch use/have used GET requests with bodies. It is a weird pattern though, I will admit. – ProgrammingLlama Apr 06 '20 at 02:17
  • @Dai Hi, I will be passing the parameters into the request's body with user input. But I am unsure of how to pass this set of data in C# code. Yes it is a GET request and I managed get the response with request body using raw. – Charis Apr 06 '20 at 06:06

0 Answers0