locked
Curl Call with C# RRS feed

  • Question

  • User-173651909 posted

    I need to make a curl call to a third party API using C# and I'm not sure how to go about it. I assume it would be using HttpClient.
    This is the curl call I need to make:

    curl -X POST -d "param1{PARAM1}&param2{PARAM2}" -H "auth-token:" https://myurl.com

    param1 and param2 are just strings to pass and I've been given a GUID for the auth token. How would you go about making the call above with C#?

    Thanks
    Adam

    Thursday, July 20, 2017 9:37 AM

All replies

  • User-335504541 posted

    Hi Adam-M,

    Please try to use this:

                string url = "https://myurl.com";
                string data = "{\"param1\":\"PARAM1\", \"param2\":PARAM2}";
                WebRequest myReq = WebRequest.Create(url);
                myReq.Method = "POST";
                myReq.ContentLength = data.Length;
                myReq.ContentType = "application/json; charset=UTF-8";
                UTF8Encoding enc = new UTF8Encoding();
                myReq.Headers.Remove("auth-token");
                using (Stream ds = myReq.GetRequestStream())
                {
                    ds.Write(enc.GetBytes(data), 0, data.Length);
                }            
                WebResponse wr = myReq.GetResponse();
                Stream receiveStream = wr.GetResponseStream();
                StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
                string content = reader.ReadToEnd();
                Response.Write(content);

    And you could refer to following links for more information:

    https://stackoverflow.com/questions/7929013/making-a-curl-call-in-c-sharp

    https://stackoverflow.com/questions/16619065/curl-request-with-asp-net

    Best Regards,

    Billy

    Friday, July 21, 2017 7:21 AM
  • User-140397749 posted

    This tool is exactly what you want: https://curl.olsh.me/

    Tuesday, April 20, 2021 8:05 AM