Send a POST request of JSON content-type and display JSON response in VB.NET

10,073

Try this instead:

Dim postdata As String = "{""amount"":" + TextBox1.Text + "},{""withdrawal"":""" + TextBox2.Text + """},{""pair"":""btc_eth""},{""returnAddress"":""" + TextBox3.Text + """}"

Your data was malformed.

Or here is another version with String.Format:

Dim postdata2 As String = String.Format("{{""amount"":{0}}},{{""withdrawal"":""{1}""}},{{""pair"":""btc_eth""}},{{""returnAddress"":""{2}""}}", TextBox1.Text, TextBox2.Text, TextBox3.Text)
Share:
10,073
Anjum
Author by

Anjum

Updated on December 05, 2022

Comments

  • Anjum
    Anjum over 1 year

    I am trying to send a POST request to shapeshift which has few parameters to be sent as JSON and then wish to display part of the response in VB.NET

    Documentation from shapeshift: https://info.shapeshift.io/api#api-9

    Below is what I have tried until now:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse
        Dim reader As StreamReader
        Dim rawresponse As String
    
        Try
    
            request = DirectCast(WebRequest.Create("https://shapeshift.io/sendamount"), HttpWebRequest)
            request.ContentType = "application/json"
            request.Method = "POST"
    
            Dim postdata As String = "{""amount"":}" + TextBox1.Text + "{,""withdrawal"":}" + TextBox2.Text + "{,""pair"":""btc_eth""}" + "{,""returnAddress"":}" + TextBox3.Text
            request.ContentLength = postdata.Length
    
            Dim requestWriter As StreamWriter = New StreamWriter(request.GetRequestStream())
            requestWriter.Write(postdata)
            requestWriter.Close()
    
            response = DirectCast(request.GetResponse(), HttpWebResponse)
            reader = New StreamReader(response.GetResponseStream())
    
    
            rawresponse = reader.ReadToEnd()
    
       Catch ex As Exception
            Console.WriteLine(ex.ToString)
       End Try
    
       Dim json As String = rawresponse
       Dim jsonObject As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(json)
    
       Label1.Text = jsonObject("expiration").ToString
    
    End Sub
    

    ERROR I get is: 400 Bad Request

    I think its because I have messed up something in code where JSON POST request is explained. I did a lot of research and tried few things but nothing worked :(

  • Anjum
    Anjum almost 6 years
    Thanks for the solution. Appreciate it.Its giving me an error: System.ArgumentNullException HResult=0x80004003 Message=Value cannot be null. Parameter name: s Source=mscorlib
  • Anjum
    Anjum almost 6 years
    It was helpful to some extent. I didnt get any error this time but label1.text was not filled with anything, So a blank response or we are doing something wrong.
  • dbl4k
    dbl4k almost 6 years
    I wonder, if you place a breakpoint on the responsebody variable (the response body as a string) does that give you any indication of the content of the message? I was hoping, after trying it myself and getting an "error" property with an error message, that including that might help but see what you get :)
  • Anjum
    Anjum almost 6 years
    info.shapeshift.io/api#api-9 this is what I am trying to achieve i.imgur.com/Ek2i1HD.png
  • dbl4k
    dbl4k almost 6 years
    oh right! Well i'd look at extending the MyRequestData + MyResponseData with the other properties listed in that documentation example first. See what you get. But importantly, it might be worth looking at the response as a string to establish what is being returned before it converts it to an object lower down.
  • Anjum
    Anjum almost 6 years
    I got one error which can help resolve this issue when I did breakpoints on "responsebody" "{""error"":""Missing a Output Amount or Deposit Amount""}" Let me figure out, I think I got what I am doing wrong. Let me try something.
  • Anjum
    Anjum almost 6 years
    Tried a lot but now getting error "Bad Request". Tried both the solutions provided in this question thread. Sending data as per example on shapeshift website still getting error: example data {"amount":123, "withdrawal":"123ABC", "pair":"ltc_btc", returnAddress:"BBBBBBB"}