Max Tyack
Max Tyack

Reputation: 31

Submit Form POST using VB .NET

I have searched a solution to my problem extensively, and while I found answers that seemed to have worked for others, I am just having a real hard time figuring this out. But I feel I am very close.

I am trying to make an Rest API call to an online application called Zoho Creator. I am trying to implement the Add Record call. The example they give is using an HTML form with a submit button. But I need to Add Records from a VB .NET desktop application. I have tried both WebClient and WebRequest, but I am unsuccessful in those attempts. But I have been successful using these methods with other API calls and other APIs, it's just this Add Records one that is giving me trouble.

One of the required parameters is an authtoken, which for security reasons I replaced with "xxxxxxxxxx". Here is an html form that I created and when I use it, it created the record successfully thru the API, it's just adding a single record with the single field value for "TicketID".

<!DOCTYPE html>
<html>
  <body>
   <form method="POST" action="https://creator.zoho.com/api/max1stdirectcom/xml/service-orders/form/Ticket_form/record/add">
   <input type="hidden" name="authtoken" value="xxxxxxxxxx"/>
   <input type="hidden" name="scope" id="scope" value="creatorapi"/>
   <input type="text" name="TicketID" value="123"/>
   <input type="submit" value="Add Record"/>
</form>
<body>

So, the above works perfectly fine. now, here is my VB .NET code trying to replicate the same result using WebRequest:

Protected Sub PostTo(sTicketID As String)
    Dim url As String = "https://creator.zoho.com/api/max1stdirectcom/xml/service-orders/form/Ticket_form/record/add"
    Dim request As WebRequest = WebRequest.Create(url)

    request.Method = "POST"

    ' Create POST data and convert it to a byte array.  
    Dim postData As String = "?authtoken=" & "xxxxxxxxxx" & "?scope=creatorapi" & "?TicketID=" & sTicketID
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)

    ' Set the ContentType property of the WebRequest.  
    request.ContentType = "application/x-www-form-urlencoded"

    ' Set the ContentLength property of the WebRequest.  
    request.ContentLength = byteArray.Length

    ' Get the request stream.  
    Dim dataStream As Stream = request.GetRequestStream()

    ' Write the data to the request stream.  
    dataStream.Write(byteArray, 0, byteArray.Length)

    ' Close the Stream object.  
    dataStream.Close()

    ' Get the response.  
    Dim response As WebResponse = request.GetResponse()

    ' Display the status.  
    Debug.WriteLine(CType(response, HttpWebResponse).StatusDescription)

    ' Get the stream containing content returned by the server.  
    dataStream = response.GetResponseStream()

    ' Open the stream using a StreamReader for easy access.  
    Dim reader As New StreamReader(dataStream)

    ' Read the content.  
    Dim responseFromServer As String = reader.ReadToEnd()
    ' Display the content.  

    Debug.WriteLine(responseFromServer)

    ' Clean up the streams.  
    reader.Close()
    dataStream.Close()
    response.Close()

End Sub

The response I get from the call using the above VB .Net code is:

<response>
  <errorlist>
    <error>
      <code>2899</code>
      <message><![CDATA[Permission Denied To Add Record(s).]]></message>
    </error>
  </errorlist>
</response>

So it is obviously making good communication with the API on some level. I am using the correct AuthToken, so not sure why it is rejecting the adding of the record. I am passing the same exact "credentials" as the basic form POST, but getting different result.

Any recommendations for me to try?

Upvotes: 1

Views: 9556

Answers (1)

Kalyan Nakka
Kalyan Nakka

Reputation: 1

Below is a working code in VB .Net

Please check and add the missing implementation in your code.

Private Sub HTTPRestPOST (ByVal JsonInputStr As String, ByVal POSTUri As String)

    'Make a request to the POST URI

    Dim RestPOSTRequest As HttpWebRequest = HttpWebRequest.Create(POSTUri)

    'Convert the JSON Input to Bytes through UTF8 Encoding

    Dim JsonEncoding As New UTF8Encoding()

    Dim JsonBytes As Byte() = JsonEncoding.GetBytes(JsonInputStr)

    'Setting the request parameters

    RestPOSTRequest.Method = "POST"

    RestPOSTRequest.ContentType = "application/json"

    RestPOSTRequest.ContentLength = JsonBytes.Length

    'Add any other Headers for the URI

    RestPOSTRequest.Headers.Add("username", "kalyan_nakka")

    RestPOSTRequest.Headers.Add("password", "********")

    RestPOSTRequest.Headers.Add("urikey", "MAIJHDAS54ADAJQA35IJHA784R98AJN")

    'Create the Input Stream for the URI

    Using RestPOSTRequestStream As Stream = RestPOSTRequest.GetRequestStream()

        'Write the Input JSON data into the Stream

        RestPOSTRequestStream.Write(JsonBytes, 0, JsonBytes.Length)

        'Response from the URI

        Dim RestPOSTResponse = RestPOSTRequest.GetResponse()

        'Create Stream for the response

        Using RestPOSTResponseStream As Stream = RestPOSTResponse .GetResponseStream()

            'Create a Reader for the Response Stream

            Using RestPOSTResponseStreamReader As New StreamReader(RestPOSTResponseStream)

                Dim ResponseData = RestPOSTResponseStreamReader.ReadToEnd()

                'Later utilize "ResponseData" variable as per your requirement

                'Close the Reader

                RestPOSTResponseStreamReader.Close()

            End Using

            RestPOSTResponseStream.Close()  

        End Using       

        RestPOSTRequestStream.Close()

    End Using 

End Sub

Upvotes: 0

Related Questions