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": "eddard@company.com", "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 = "login=eddard@company.com&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!

user1146024
  • 13
  • 2
  • 4
  • Your post string should be url encoded (see HttpUtility.UrlEncode() or an example here http://stackoverflow.com/a/14703032/99256) for starters. – MartyIX Nov 21 '13 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. – user1146024 Nov 22 '13 at 14:11

1 Answers1

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);
MartyIX
  • 27,828
  • 29
  • 136
  • 207
  • Thanks for the response. I've changed the line recommended but still the same results. – user1146024 Nov 22 '13 at 14:10
  • I would recommend then to install Fiddler (http://fiddler2.com/home) and have a look at requests from your application ( http://stackoverflow.com/questions/935181/how-to-view-the-headers-sent-by-httpwebrequest). This way you can compare headers from your .NET application and headers from curl. – MartyIX Nov 22 '13 at 15:15