Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use HttpClient PostAsync parameters properly?

So I am working on writing an extension class for my project using HttpClient since I am moving over from HttpWebRequest.

When doing the POST request, how do I send a normal string as a parameter? No json or anything just a simple string.

And this is what it looks like so far.

static class HttpClientExtension
    {
        static HttpClient client = new HttpClient();
        public static string GetHttpResponse(string URL)
        {
            string fail = "Fail";
            client.BaseAddress = new Uri(URL);
            HttpResponseMessage Response = client.GetAsync(URL).GetAwaiter().GetResult();
            if (Response.IsSuccessStatusCode)
                return Response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
            else
                return fail;
        }

        public static string PostRequest(string URI, string PostParams)
        {
            client.PostAsync(URI, new StringContent(PostParams));
            HttpResponseMessage response = client.GetAsync(URI).GetAwaiter().GetResult();
            string content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
            return content;
        }
    }

If you look at this like

client.PostAsync(URI, new StringContent(PostParams));

You can see that I just tried creating new StringContent and passing a string into it and the response returned 404 page not found. How do I properly use Post.Async(); do I send a string or byte array? Because with HttpWebRequest you would do it like this

public static void SetPost(this HttpWebRequest request, string postdata)
        {
            request.Method = "POST";
            byte[] bytes = Encoding.UTF8.GetBytes(postdata);

            using (Stream requestStream = request.GetRequestStream())
                requestStream.Write(bytes, 0, bytes.Length);
        }
like image 585
Aleks Slade Avatar asked Dec 22 '17 16:12

Aleks Slade


People also ask

How do I use PostAsync client?

Using SendAsync, we can write the code as: static async Task SendURI(Uri u, HttpContent c) { var response = string. Empty; using (var client = new HttpClient()) { HttpRequestMessage request = new HttpRequestMessage { Method = HttpMethod. Post, RequestUri = u, Content = c }; HttpResponseMessage result = await client.

What is HttpClient PostAsync?

PostAsync(String, HttpContent) Send a POST request to the specified Uri as an asynchronous operation. PostAsync(Uri, HttpContent) Send a POST request to the specified Uri as an asynchronous operation.

What is HttpClient SendAsync?

SendAsync(HttpRequestMessage) Send an HTTP request as an asynchronous operation. SendAsync(HttpRequestMessage, HttpCompletionOption) Send an HTTP request as an asynchronous operation.


2 Answers

In the PostRequest the following is done..

client.PostAsync(URI, new StringContent(PostParams));
HttpResponseMessage response = client.GetAsync(URI).GetAwaiter().GetResult();

Which does not capture the response of the POST.

Refactor to

public static string PostRequest(string URI, string PostParams) {            
    var response = client.PostAsync(URI, new StringContent(PostParams)).GetAwaiter().GetResult();
    var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
    return content;
}

HttpClient is primarily meant to be used async so consider refactoring to

public static async Task<string> PostRequestAsync(string URI, string PostParams) {            
    var response = await client.PostAsync(URI, new StringContent(PostParams));
    var content = await response.Content.ReadAsStringAsync();
    return content;
}
like image 104
Nkosi Avatar answered Sep 22 '22 17:09

Nkosi


You need prepare object and then you will serialize the object using Newtonsoft.Json. After that you will prepare byte content from the buffer. We are using api url api/auth/login and it is not full api url as we used dependency injection and configure base address in startup, see the second code.

public async void Login(string username, string password)
    {
        LoginDTO login = new LoginDTO();
        login.Email = username;
        login.Password = password;
        var myContent = JsonConvert.SerializeObject(login);
        var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
        var byteContent = new ByteArrayContent(buffer);
        byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        var response = await httpClient.PostAsync("api/auth/login", byteContent);
        var contents = await response.Content.ReadAsStringAsync();
    }
services.AddHttpClient<IAuthService, AuthService>(client =>
        {
            client.BaseAddress = new Uri("https://localhost:44354/");
        });

.NET 5 Solution

In .NET 5, There is new class JsonContent and you can implement this easily

LoginDTO login = new LoginDTO();
login.Email = username;
login.Password = password;

JsonContent content = JsonContent.Create(login);
var url = "http://...";
HttpResponseMessage response = await httpClient.PostAsync(url, content);
like image 23
Khabir Avatar answered Sep 22 '22 17:09

Khabir