WebRequest Equivalent to CURL command

Based on Nikolaos's pointers I appear to have fixed this with the following code:

public static gta_allCustomersResponse gta_AllCustomers()
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.somewhere.com/desk/external_api/v1/customers.json");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Accept = "*/*";
        httpWebRequest.Method = "GET";
        httpWebRequest.Headers.Add("Authorization", "Basic reallylongstring");

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            gta_allCustomersResponse answer =  JsonConvert.DeserializeObject<gta_allCustomersResponse>(streamReader.ReadToEnd());
            return answer;
        }
    }

This is the curl command I use to post json data:

curl http://IP:PORT/my/path/to/endpoint -H 'Content-type:application/json' -d '[{...json data...}]'

This is equivalent to the above curl command with C#:

var url = "http://IP:PORT/my/path/to/endpoint";
var jsonData = "[{...json data...}]";

using (var client = new WebClient())
{
    client.Headers.Add("content-type", "application/json");
    var response = client.UploadString(url, jsonData);
}

Here is my solution to post json data to using an API call or webservice

    public static void PostJsonDataToApi(string jsonData)
    {

        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.somewhere.com/v2/cases");
        httpWebRequest.ReadWriteTimeout = 100000; //this can cause issues which is why we are manually setting this
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Accept = "*/*";
        httpWebRequest.Method = "POST";
        httpWebRequest.Headers.Add("Authorization", "Basic ThisShouldbeBase64String"); // "Basic 4dfsdfsfs4sf5ssfsdfs=="
        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
           // we want to remove new line characters otherwise it will return an error
            jsonData= thePostBody.Replace("\n", ""); 
            jsonData= thePostBody.Replace("\r", "");
            streamWriter.Write(jsonData);
            streamWriter.Flush();
            streamWriter.Close();
        }

        try
        {
            HttpWebResponse resp = (HttpWebResponse)httpWebRequest.GetResponse();
            string respStr = new StreamReader(resp.GetResponseStream()).ReadToEnd();
            Console.WriteLine("Response : " + respStr); // if you want see the output
        }
        catch(Exception ex)
        {
         //process exception here   
        }

    }

Tags:

C#

Curl