Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL with user authentication in C#

I want to do the following cURL request in c#:

curl -u admin:geoserver -v -XPOST -H 'Content-type: text/xml' \
   -d '<workspace><name>acme</name></workspace>' \
   http://localhost:8080/geoserver/rest/workspaces

I have tried using a WebRequest:

string url = "http://localhost:8080/geoserver/rest/workspaces";
WebRequest request = WebRequest.Create(url);

request.ContentType = "Content-type: text/xml";
request.Method = "POST";
request.Credentials = new NetworkCredential("admin", "geoserver");

byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes("<workspace><name>my_workspace</name></workspace>");
Stream reqstr = request.GetRequestStream();
reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();

WebResponse response = request.GetResponse();
...

But I get an error: (400) Bad request.

If I change the request credentials and add the authentication in the header:

string url = "http://localhost:8080/geoserver/rest/workspaces";
WebRequest request = WebRequest.Create(url);

request.ContentType = "Content-type: text/xml";
request.Method = "POST";
string authInfo = "admin:geoserver";
request.Headers["Authorization"] = "Basic " + authInfo;

byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes("<workspace><name>my_workspace</name></workspace>");
Stream reqstr = request.GetRequestStream();
reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();

WebResponse response = request.GetResponse();
...

Then I get: (401) Unauthorised.

My question is: Should I use another C# class like WebClient or HttpWebRequest or do I have to use the curl bindings for .NET?

All comments or guidance would be appreciated.

like image 411
taudorf Avatar asked Mar 01 '11 09:03

taudorf


People also ask

How do you pass authentication in curl?

To send basic auth credentials with Curl, use the "-u login: password" command-line option. Curl automatically converts the login: password pair into a Base64-encoded string and adds the "Authorization: Basic [token]" header to the request.

What authentication does curl use?

This will make curl use the default "Basic" HTTP authentication method. Yes, it is actually called Basic and it is truly basic. To explicitly ask for the basic method, use --basic .

How do I send a bearer token in curl?

To send a bearer token to the server, you can use the 'Authorization: Bearer {token}' authorization header. The 'Accept: application/json' header tells the server that the client expects JSON. The server informs the client that it has returned JSON with a 'Content-Type: application/json' response header.

What is -- user in curl?

--user (or -u ) in curl provides a basic auth to your request. In Postman you can achieve the same result with a choice in Authorization tab. --user "<client_id>:<client_secret>" becomes. Type: Basic Auth. Username: client_id.


2 Answers

The solution to my question was changing the ContentType property. If I change the ContentType to

request.ContentType = "text/xml";

the request works in both cases, if I also convert the authInfo to a Base64String in the last example like Anton Gogolev suggested.

like image 82
taudorf Avatar answered Sep 26 '22 09:09

taudorf


Using:

request.ContentType = "application/xml";

request.Credentials = new NetworkCredential(GEOSERVER_USER, GEOSERVER_PASSWD);

also works. The second sets authentication information.

like image 45
okello Avatar answered Sep 22 '22 09:09

okello