Just getting started and am trying to test getting the Session Key using VB.NET. However, I cannot get it to work and am getting the error "(400) Bad Request". I was able to get this to work in Excel VBA, so I know the URL, user name , and password are correct and that I have the concept down. I just can't get it to work in VB.NET.
My code is below. I noted which line throws the error.
Sub Metabase_GetSessionToken()
Dim vURI As String, vPOSTBODY As String
vURI = "https://mycompanymetbaseurl.com/api/session"
vPOSTBODY = "{""username"": ""mymetabaseusername"", ""password"": ""mymetabasepassword""}"
Dim vBYTESDATA As Byte() = Text.Encoding.UTF8.GetBytes(vPOSTBODY)
Dim vHTTPREQUEST As HttpWebRequest = CType(WebRequest.Create(vURI), HttpWebRequest)
vHTTPREQUEST.Method = "POST"
vHTTPREQUEST.ContentType = "application/x-www-form-urlencoded"
vHTTPREQUEST.Accept = "application/json"
vHTTPREQUEST.ContentLength = vBYTESDATA.Length
Dim vSTREAMOBJECT As Stream = vHTTPREQUEST.GetRequestStream()
vSTREAMOBJECT.Write(vBYTESDATA, 0, vBYTESDATA.Length)
vSTREAMOBJECT.Close()
Dim vHTTPRESPONSE As HttpWebResponse
'The error occurs at the following line
vHTTPRESPONSE = CType(vHTTPREQUEST.GetResponse(), HttpWebResponse)
Dim vSTREAMREADER As New StreamReader(vHTTPRESPONSE.GetResponseStream())
Dim vSTREAMDATA As String = vSTREAMREADER.ReadToEnd
Response.Write(vSTREAMDATA)
End Sub