2

I keep getting a 404 error. It works fine if I call the REST API from SoapUI.

I’m using Visual Basic VS2015. I have a sample function which I’m calling from a simple forms project. This is just to get the REST API to work. The REST API call will go into a Visual Basic Windows Service once I get it working,

There is a form named form1 which has a txtURL textbox, a button to call sub Main(), and an output textbox called textbox1.

Public Shared Sub Main()
        Try
            Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(Form1.txtURL.Text), HttpWebRequest)
            With myHttpWebRequest
                .Method = "POST"
                .ContentType = "application/json"
                .Accept = "application/json"
                .MediaType = "jsonp"
 
                With .Headers
                    .Add("Authorization", "Bearer ABCDabcd1234=")
                    .Add("riskLevelStatus", "6897")
                    .Add("userId", "12345")
                    .Add("applicationName", "MyApp")
                End With
 
            End With
            Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
 
            Form1.TextBox1.Text = Form1.TextBox1.Text & myHttpWebResponse.ToString() & vbCrLf
 
            myHttpWebResponse.Close()
            'Catch exception if trying to add a restricted header.
        Catch e As ArgumentException
            Form1.TextBox1.Text = Form1.TextBox1.Text & "Error-ArgumentException: " & e.Message & vbCrLf
        Catch e As WebException
            Form1.TextBox1.Text = Form1.TextBox1.Text & "Error-WebException: " & e.Message & vbCrLf
            If e.Status = WebExceptionStatus.ProtocolError Then
                Form1.TextBox1.Text = Form1.TextBox1.Text & "Error-Status Code: " & CType(e.Response, HttpWebResponse).StatusCode & vbCrLf
                Form1.TextBox1.Text = Form1.TextBox1.Text & "Error-Status Description: " & CType(e.Response, HttpWebResponse).StatusDescription & vbCrLf
                Form1.TextBox1.Text = Form1.TextBox1.Text & "Error-Server: " & CType(e.Response, HttpWebResponse).Server & vbCrLf
            End If
        Catch e As Exception
            Form1.TextBox1.Text = Form1.TextBox1.Text & "Error-Exception: " & e.Message & vbCrLf
        End Try
    End Sub 'Main

Here is what is outputted to textbox1:

Error -WebException: The remote server returned an Error: (400) Bad Request.

Error -Status Code: 400

Error -Status Description

Error -Server

What should be returned is a single line JSON, similar to this:

{“quid”: “jhgdsjdshg-hdbw-akjhjk-kdhbfsihg”}

It works fine when calling from SoapUI.

I believe this issue is how do I add data to the body?

Community
  • 1
  • 1
David Who
  • 101
  • 1
  • 1
  • 4

1 Answers1

8

I figured it out. I cannot believe nobody had an answer.

  Public Sub Try01(URL)
        Try
            Dim myReq As HttpWebRequest
            Dim myResp As HttpWebResponse
            Dim myReader As StreamReader
            myReq = HttpWebRequest.Create(URL)
            myReq.Method = "POST"
            myReq.ContentType = "application/json"
            myReq.Accept = "application/json"
            myReq.Headers.Add("Authorization", "Bearer LKJLMLKJLHLMKLJLM839800K=")
            Dim myData As String = "{""riskLevelStatus"":""0001"",""userId"":""10000004030"",""applicationName"":""MyTestRESTAPI""}"
            myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count)
            myResp = myReq.GetResponse
            myReader = New System.IO.StreamReader(myResp.GetResponseStream)
            TextBox1.Text = myReader.ReadToEnd
        Catch ex As Exception
            TextBox1.Text = TextBox1.Text & "Error: " & ex.Message
        End Try
    End Sub
clavarreda
  • 21
  • 3
David Who
  • 101
  • 1
  • 1
  • 4