CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2018
    Posts
    3

    CURL POST request in VB.NET (json)

    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).

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,820

    Re: CURL POST request in VB.NET (json)

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured