Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebRequest Equivalent to CURL command

Tags:

c#

curl

I am banging my head against a wall trying to convert a working curl command to a c# WebRequest.

I have read through quite a few postings and I was pretty sure I had the code right but it still will not work.

Can anyone see what I am doing wrong please?

Here is the working curl command:

curl -k -u x:reallylongstring -H "Content-Type: application/json"  https://api.somewhere.com/desk/external_api/v1/customers.json

And this is the code I have written in c#:

WebRequest wrGETURL;
wrGETURL = WebRequest.Create("https://api.somewhere.com/desk/external_api/v1/customers.json");
wrGETURL.Method = "GET";
wrGETURL.ContentType = "application/json"; 
wrGETURL.Credentials = new NetworkCredential("x", "reallylongstring");
Stream objStream = wrGETURL.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream);
string responseFromServer = objReader.ReadToEnd();

But the api responds:

The remote server returned an error: (406) Not Acceptable.

Any help would be much appreciated!

Thanks

like image 392
Trevor Daniel Avatar asked Jan 21 '14 10:01

Trevor Daniel


People also ask

What is the alternative for Curl in Windows?

Other interesting Windows alternatives to cURL are aria2, Postman, HTTPie for Terminal and xh. cURL alternatives are mainly HTTP Clients but may also be Download Managers or API Clients. Filter by these if you want a narrower list of alternatives or looking for a specific functionality of cURL.

Is there a curl for Windows?

cURL is free, open software that runs under various operating systems. This tutorial demonstrates cURL on a Windows 64-bit operating system that is enabled for the secure sockets layer (SSL).

What is curl GET command?

13. GET method. The GET method is used to retrieve resources from a particular URL. The simple curl https://www.keycdn.com command will use GET as the default HTTP method, however it can also be specified using --request GET or -X GET .

Does Curl post by default?

By default you use curl without explicitly saying which request method to use. If you just pass in a HTTP URL like curl http://example.com it will use GET. If you use -d or -F curl will use POST, -I will cause a HEAD and -T will make it a PUT.


1 Answers

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;
        }
    }
like image 173
Trevor Daniel Avatar answered Oct 12 '22 09:10

Trevor Daniel