0

I need to implement a post request in a c# winform application of my project. Earlier to that I just have implemented get requests. I have checked that the API URI is working well (I checked it using Postman). I never implemented POST requests in the past. The get requests I implement using the following code:

WebClient n = new WebClient();
string uri = "API_URI";
string json = n.DownloadString(uri);

Now my requirement is to download json string using post method with an "apikey" with its value which I need to provide while calling the URI.

When I am using the above code, it is searching the "API_URI" in my local application directory.

Any direction, sample code and or tutorial will be much appreciated. Please help me with that.

Trevor
  • 7,777
  • 6
  • 31
  • 50
DeepakVerma
  • 99
  • 10

3 Answers3

0

Since you have the call tested in Postman, as a starting point use the "Code" link in PostMan to generate your call using RestSharp so that you can test it and further refine it.

https://learning.getpostman.com/docs/postman/sending-api-requests/generate-code-snippets/

P. Roe
  • 2,077
  • 1
  • 16
  • 23
0

you can do something like this:

WebClient client = new WebClient();
string uri = "API_URI";
string json = "{some:\"json data\"}";
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
client.Headers.Add("Authorization", "apikey");
string response = client.UploadString(uri,json);

this is the documentation https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.uploadstring?view=netframework-4.8

0

You can use POST method in this way

WebClient client = new WebClient();
string uri = "API_URI";
var reqparm=new NameValueCollection(); // Used for passing request perameter
reqparm.Add("some","json data");
response = Encoding.UTF8.GetString(client.UploadValues(uri, "POST", reqparm));

I hope this will help you.

Hiren Patel
  • 1,071
  • 11
  • 34