I've been struggling to send this CURL request in VB.NET and hope someone can help..
Here is a link to the documentation of the exact request I'm trying to send.
This is the sample CURL request:
Code:
curl -u 'username:token' 'https://api.dev.name.com/v4/domains' -X POST -H 'Content-Type: application/json' --data '{"domain":{"domainName":"example.org"},"purchasePrice":12.99}'
Here is the code that I put together and tested:
Code:
Dim uri As New Uri("https://api.dev.name.com/v4/domains")
Dim myUsername As String = txtUsername.Text.Trim & ":" & txtApiKey.Text.Trim
Dim data As String = "{""domain"":{""domainName"":""example.org""},""purchasePrice"":12.99}"
Dim dataByte As Byte()
request = WebRequest.Create(uri)
request.Headers.Add(HttpRequestHeader.Authorization, "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes(myUsername)))
request.ContentType = "application/json"
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
request.Method = "POST"
dataByte = Encoding.UTF8.GetBytes(data)
request.GetRequestStream.Write(dataByte, 0, dataByte.Length)
Dim myResp As HttpWebResponse
myResp = request.GetResponse()
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
WebResponse = myreader.ReadToEnd()
MsgBox(WebResponse)
However, the code above always returns a (400) Bad Request.
Here is another variation of pretty much the same code which I tried:
Code:
Dim myReq As HttpWebRequest
Dim myResp As HttpWebResponse
Dim myUsername As String = "username:token"
myReq = HttpWebRequest.Create("https://api.dev.name.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)
WebResponse = myreader.ReadToEnd()
MsgBox(WebResponse)
But, as you can already guess, it also returns a 400 (Bad Request).