0

Hi have the following cURL command that I would like to convert to VB.NET or C#.

curl https://api.box.com/2.0/users \-H "Authorization: Bearer ACCESS_TOKEN" \ -d '{"login": "[email protected]", "name": "Ned Stark"}' \ -X POST

I've tried the following in VB.NET:

Dim url As String = "https://api.box.com/2.0/users"
Dim wrq = CType(Net.WebRequest.Create(url), HttpWebRequest)
Dim postString As String = "[email protected]&name=Ned Stark"

wrq.Headers.Add("Authorization: Bearer " & o2ses.AccessToken)
wrq.Method = "POST"
wrq.ContentType = "application/x-www-form-urlencoded"
wrq.ContentLength = postString.Length

Dim wrqWriter As New StreamWriter(wrq.GetRequestStream())
wrqWriter.Write(postString)
wrqWriter.Close()

Dim responseReader As New StreamReader(wrq.GetResponse().GetResponseStream())
Dim responseData As String = responseReader.ReadToEnd()

I am a newb to cURL and to using HttpWebRequest in .NET, but have converted cURL commands to .NET before (thanks to this site :)), but none with the -d option in them, so I'm kind of lost. It keeps returning error 400 (bad request) when the line below is executed.

Dim responseReader As New StreamReader(wrq.GetResponse().GetResponseStream())

I think it has to do with what data I am sending (i.e. it's not getting the proper 'login' and 'name' parameters it needs).

Any help is appreciated. I can try to provide more information if needed. As mentioned, I'm a newb to this, so I'll try my best.

Thanks!

2
  • Your post string should be url encoded (see HttpUtility.UrlEncode() or an example here stackoverflow.com/a/14703032/99256) for starters. Commented Nov 21, 2013 at 23:24
  • Thanks for the response and link. I modified the code to define 'poststring' as stringbuilder and appended the urlencoded data but I still get a 'bad request (400)' from the web service. Commented Nov 22, 2013 at 14:11

1 Answer 1

0

I do not know vb.net but I think that the authorization header should look like this:

wrq.Headers.Add(HttpRequestHeader.Authorization, "Bearer " & o2ses.AccessToken);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. I've changed the line recommended but still the same results.
I would recommend then to install Fiddler (fiddler2.com/home) and have a look at requests from your application ( stackoverflow.com/questions/935181/…). This way you can compare headers from your .NET application and headers from curl.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.