Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Post Request from Windows Form C#

Currently I pass information to a Web API through the curl command as below:

curl -d 'info={ "EmployeeID": [ "1234567", "7654321" ], "Salary": true, "BonusPercentage": 10}' http://example.com/xyz/php/api/createjob.php

This will return me another URL to the API posting all the information here over there:

http://example.com/xyz#newjobapi:id=19

I am trying to replicate this process through a C# Windows form where in users will enter the required information and once they submit, they should get the returned URL.

I have already created the interface for users to enter this information. But i am not sure how to post this info to the Web API and get the generated url

Is there any library that i can use to replicate the above curl process through Windows Form??

like image 776
blackfury Avatar asked Oct 26 '25 16:10

blackfury


1 Answers

        HttpWebRequest webRequest;

        string requestParams = ""; //format information you need to pass into that string ('info={ "EmployeeID": [ "1234567", "7654321" ], "Salary": true, "BonusPercentage": 10}');

                webRequest = (HttpWebRequest)WebRequest.Create("http://example.com/xyz/php/api/createjob.php");

                webRequest.Method = "POST";
                webRequest.ContentType = "application/json";

                byte[] byteArray = Encoding.UTF8.GetBytes(requestParams);
                webRequest.ContentLength = byteArray.Length;
                using (Stream requestStream = webRequest.GetRequestStream())
                {
                    requestStream.Write(byteArray, 0, byteArray.Length);
                }

                // Get the response.
                using (WebResponse response = webRequest.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        StreamReader rdr = new StreamReader(responseStream, Encoding.UTF8);
                        string Json = rdr.ReadToEnd(); // response from server

                    }
                }
like image 59
Leon Barkan Avatar answered Oct 28 '25 06:10

Leon Barkan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!