0

I am using below code to do a POST request on a url.

namespace ConsoleAppPost
{
    using System;
    using System.Net.Http;
    using System.Threading.Tasks;

    namespace HttpClientStatus
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                using var client = new HttpClient();

                var result = await client.PostAsync("https://XXXX/api/XXX/XX/XXX/xyz", HttpContent content);
                Console.WriteLine(result.StatusCode);


            }
        }
    }
}

But ,i am getting below error:

No overload for method PostAsync takes 1 argument

I also want to use username and password as authentication to do POST request in the code. Please help me.

1
  • 1
    you're not passing any data into the second PostAsync param.. Commented Nov 22, 2019 at 15:44

1 Answer 1

1

The issue lies here

var result = await client.PostAsync("https://XXXX/api/XXX/XX/XXX/xyz", HttpContent content);

You are passing type HttpContent content as parameter and NOT the data instead?

You need to do something like this

var content = new FormUrlEncodedContent(new[]
{
    new KeyValuePair<string, string>("Username", "<value>"),
    new KeyValuePair<string, string>("Password", "<value>"),
});
var result = await client.PostAsync("https://XXXX/api/XXX/XX/XXX/xyz", content);
Sign up to request clarification or add additional context in comments.

7 Comments

Now, I am getting below error: Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'KeyValuePair<,>' could not be found (are you missing a using directive or an assembly reference?)
@PenguinTech use System.Collections.Generic namespace
Thanks a lot. It works now. But i am unable to print any response on command line though i use Console.Readline : var result = await client.PostAsync("XXXXX", content); Console.ReadLine();
What is the error you are getting? Also are you expecting from your post request?
I am not getting any error. In the console ,i am not getting any output printed . I am expecting the response similar to when i do the post request from browser .
|

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.