CURL request in VB.net

mixing

Regular Member
Joined
Jan 18, 2014
Messages
412
Reaction score
63
I'm trying to send a CURL request, but appear to be having some issues with my header and hope someone can help...

Code:
curl -u 'myusername-test:1815dff0d321430378567bc84963ecd06f71d10f' 'https://api.dev.thewebsite.com/v4/domains' -X POST -H 'Content-Type: application/json' --data '{"domain":{"domainName":"example.org"},"purchasePrice":12.99}'

Here is my code:
Code:
Try
Dim myReq As HttpWebRequest
Dim myResp As HttpWebResponse
Dim myUsername As String = "myusername-test:1815dff0d321430378567bc84963ecd06f71d10f"

myReq = HttpWebRequest.Create("https://api.dev.thewebsite.com/v4/domains")

myReq.Method = "POST"
myReq.ContentType = "application/json"
myReq.Headers.Add(myUsername)
Dim myData As String = "{""domain"":{""domainName"":""example.org""},""purchasePrice"":12.99}"
myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count)
myResp = myReq.GetResponse
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
WebResponse = myreader.ReadToEnd
Catch ex As Exception
txtLog.AppendText("THREAD EXCEPTION at " & TimeOfDay & " with error: " & ex.Message & vbCrLf)
End Try

This looks to me like it should work, but I keep getting an exception:
THREAD EXCEPTION at 4:05:33 AM with error: Specified value does not have a ':' separator.
 
Have you tried ChatGPT? No experience in VB.NET but here is the response:

The error is occurring because you are trying to add the `myUsername` variable directly to the headers without specifying the header name. To fix this error, you need to specify the header name as well. Here's the corrected code:
Code:
Try
Dim myReq As HttpWebRequest
Dim myResp As HttpWebResponse
Dim myUsername As String = "myusername-test:1815dff0d321430378567bc84963ecd06f71d10f"

myReq = HttpWebRequest.Create("https://api.dev.thewebsite.com/v4/domains")

myReq.Method = "POST"
myReq.ContentType = "application/json"
myReq.Headers.Add("Authorization", "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes(myUsername)))

Dim myData As String = "{""domain"":{""domainName"":""example.org""},""purchasePrice"":12.99}"
Dim dataBytes As Byte() = Encoding.UTF8.GetBytes(myData)
myReq.GetRequestStream.Write(dataBytes, 0, dataBytes.Length)

myResp = myReq.GetResponse()
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
Dim WebResponse As String = myreader.ReadToEnd()

Catch ex As Exception
txtLog.AppendText("THREAD EXCEPTION at " & TimeOfDay & " with error: " & ex.Message & vbCrLf)
End Try

In this code, I've added the header name "Authorization" and used the "Basic" authentication scheme to add the `myUsername` to the headers. I've also modified the way the request data is written to the request stream for better readability.
 
Thanks for your reply and suggestion @TomTheCat

I tried that, but it now returns:
THREAD EXCEPTION at 4:38:10 AM with error: The remote server returned an error: (400) Bad Request.
 
Here is something else I've tried.....
1) I took my CURL request and used the curl to C# converter to convert it to C#
2) I took the C# code and used the C# to VB.net converter to convert it to VB.net
3) That gave me this:
Code:
Using httpClient = New HttpClient()
    Using request = New HttpRequestMessage(New HttpMethod("POST"), "https://api.dev.thewebsite.com/v4/domains")
        Dim base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("myusername-test:1815dff0d321430378567bc84963ecd06f71d10f"))
        request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}")
        request.Content = New StringContent("{""domain"":{""domainName"":""example.org""},""purchasePrice"":12.99}")
        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json")
        Dim response = Await httpClient.SendAsync(request)
    End Using
End Using

However, this code gives a lot of errors..
https://i.imgur.com/he7dOEj.jpg
I've done some re-writing, but continue to have issues...
Code:
                        Dim myUsername As String = "myusername-test:1815dff0d321430378567bc84963ecd06f71d10f"
                        Dim EncodedString As String = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(myUsername))

                        Dim httpWebRequest = CType(WebRequest.Create("https://api.dev.thewebsite.com/v4/domains"), HttpWebRequest)
                        httpWebRequest.Method = "Post"

                        httpWebRequest.Headers.Add("Authorization: Basic " & EncodedString)
                        httpWebRequest.ContentType = "application/json"
                        Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
                            Dim Body As String = "{""domain"":{""domainName"":""example.org""},""purchasePrice"":12.99}"
                            streamWriter.Write(Body)
                        End Using

                        Dim httpResponse = CType(httpWebRequest.GetResponse(), HttpWebResponse)
                        Using streamReader = New StreamReader(httpResponse.GetResponseStream())
                            Dim result = streamReader.ReadToEnd()
                            WebResponse = result
                        End Using
Returns a (400) Bad Request. Since this is a "Bad Request" I can't really tell if that means I'm getting authenticated okay now and the error is maybe in my Body/data?
 
Back
Top