How to Use Httpclient to Send Content in Body of Get Request

Calling Get Request with Json Body using httpclient

almost search all over and trying all the variant of using GET Method finally the solution which worked for me in this case was this

var client = new HttpClient();
client.BaseAddress = new Uri("https://baseApi/");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", string.Format("Bearer {0}", token));
var query = new Dictionary<string, string>
{
["pickup_postcode"] = 400703,
["delivery_postcode"] = 421204,
["cod"] = "0",
["weight"] = 2,
};

var url = "methodurl";
var response = await client.GetAsync(QueryHelpers.AddQueryString(url, query));
var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return JsonConvert.DeserializeObject<MyModel>(responseBody);

Got QueryHelpers from Microsoft.AspNetCore.WebUtilities package

How to use HttpClient to send content into req.body of GET request in angular?

As far as I can tell you cannot use HttpClient get to send a body. The only way is to use query.

const someObject = {example object};
http.get(url + '/?data='+ encodeURIComponent( JSON.stringify(someObject)));

How to send a Post body in the HttpClient request in Windows Phone 8?

UPDATE 2:

From @Craig Brown:

As of .NET 5 you can do:

requestMessage.Content = JsonContent.Create(new { Name = "John Doe", Age = 33 });

See JsonContent class documentation

UPDATE 1:

Oh, it can be even nicer (from this answer):

requestMessage.Content = new StringContent("{\"name\":\"John Doe\",\"age\":33}", Encoding.UTF8, "application/json");

This depends on what content do you have. You need to initialize your requestMessage.Content property with new HttpContent. For example:

...
// Add request body
if (isPostRequest)
{
requestMessage.Content = new ByteArrayContent(content);
}
...

where content is your encoded content. You also should include correct Content-type header.

How to get content body from a httpclient call?

The way you are using await/async is poor at best, and it makes it hard to follow. You are mixing await with Task'1.Result, which is just confusing. However, it looks like you are looking at a final task result, rather than the contents.

I've rewritten your function and function call, which should fix your issue:

async Task<string> GetResponseString(string text)
{
var httpClient = new HttpClient();

var parameters = new Dictionary<string, string>();
parameters["text"] = text;

var response = await httpClient.PostAsync(BaseUri, new FormUrlEncodedContent(parameters));
var contents = await response.Content.ReadAsStringAsync();

return contents;
}

And your final function call:

Task<string> result = GetResponseString(text);
var finalResult = result.Result;

Or even better:

var finalResult = await GetResponseString(text);

How can I Post parameter in httpClient where in postman use param

Use this code:

HttpClient client = new HttpClient();
var dictionary = new Dictionary<string, string>
{
{ "parameter0", "value0" },
{ "parameter1", "value1" }
};
var content = new FormUrlEncodedContent(dictionary);
var response = await client.PostAsync("https://WebServiceAddress", content);
var responseString = await response.Content.ReadAsStringAsync();


Related Topics



Leave a reply



Submit