Post JSON on URL HTTP Web Request with VB.NET

user3567163 picture user3567163 · Jun 27, 2014 · Viewed 18.3k times · Source

I am using an online NLP API called Wit.ai. I am sending an http web request, and I get a response, and that all works perfectly fine. However, now I need to know how to POST this JSON:

 {
   "state": "email_or_text"
 }

As an addition to this code:

 Function getJson()
    Dim editedText As String = TextBox1.Text.Replace(" ", "%20")
    Dim myHttpWebRequest = CType(WebRequest.Create("https://api.wit.ai/message?v=20140609&q=" + editedText + "&units=metric"), HttpWebRequest)
    myHttpWebRequest.Headers.Add("Authorization: Bearer <myauthcode>")
    Dim myHttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
    Dim myWebSource As New StreamReader(myHttpWebResponse.GetResponseStream())
    Dim myPageSource As String = myWebSource.ReadToEnd()
    Return myPageSource
End Function

Thanks for your help!

Answer

firas489 picture firas489 · Mar 6, 2015

Check this code at http://dotnetpad.com/9O883hmI it has a quick example of a POST method that can get you started on posting your JSON. Modify the URL and the content string and hit Run and see the results!