2 Replies - 677 Views - Last Post: 18 November 2017 - 11:29 AM Rate Topic: -----

#1 t3cho  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 227
  • Joined: 27-May 14

REST Api Call

Posted 16 November 2017 - 03:12 PM

Hey..

I just want to know is it possible to make this API call from vb.net or c#

https://www.zoho.com...it-records.html

I have no idea from where to start. Am i on good way to make this possible

Public Sub updateRecord(ByVal ht As Hashtable, ByVal criteriaField As String)
        Dim ticket As String = getTicket("USERNAME", "PASSWORD")
        If ticket <> "false" Then
            Dim apiUrl As String = "https://creator.zoho.com/api/xml/write/apikey=APIKEY&ticket=" + ticket
            Dim xmlStr As New System.Text.StringBuilder
            Dim newvalue As New System.Text.StringBuilder
            newvalue.AppendLine("<newvalues>")

            xmlStr.Append("<ZohoCreator><applicationlist>")
            xmlStr.Append("<application name=><formlist><form name=Ebay_Inventory>")
            xmlStr.AppendLine("<update>")
            Dim enu As IDictionaryEnumerator = ht.GetEnumerator
            While (enu.MoveNext)
                If criteriaField = enu.Key Then
                    xmlStr.AppendLine("<criteria>")
                    xmlStr.AppendLine("<field name='" + enu.Key + "' compOperator='Equals' value='" + enu.Value + "'></field>")
                    xmlStr.AppendLine("</criteria>")
                Else
                    newvalue.AppendLine("<field name='" + enu.Key + "' value='" + enu.Value + "'></field>")
                End If
            End While
            newvalue.AppendLine("</newvalues>")
            xmlStr.Append(newvalue.ToString)
            xmlStr.AppendLine("</update>")
            xmlStr.AppendLine("</form></formlist></application></applicationlist></ZohoCreator>")
            Dim params As String = "XMLString=" + xmlStr.ToString
            Dim res As String = getResponseFromUrl(apiUrl, params)
        Else
            MsgBox("Username or Password is incorrect")
        End If
    End Sub

    Public Function getTicket(ByVal user As String, ByVal password As String)
        Dim iamticketid As String = "false"
        Try
            Dim url As String = "https://accounts.zoho.com/login?FROM_AGENT=true"
            Dim params As String = "PASSWORD=x&LOGIN_ID=x"
            Dim res As String = getResponseFromUrl(url, params)
            Dim toindex As Integer = res.IndexOf("RESULT=")
            Dim fromindex As Integer = res.IndexOf("TICKET=")
            Dim length As Integer = toindex - fromindex - 8
            Dim ticketid As String = res.Substring(fromindex + 7, length)
            Dim result1 As String = res.Substring(toindex + 7, 5)
            If result1 <> "FALSE" Then
                iamticketid = ticketid
            Else
                iamticketid = "false"
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Return iamticketid.Trim()
    End Function

    Public Function getResponseFromUrl(ByVal url As String, ByVal params As String)
        Dim str As String = ""
        Try
            Dim webreq As HttpWebRequest = WebRequest.Create(url)
            webreq.Method = "POST"
            webreq.ContentType = "application/x-www-form-urlencoded"
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(params)
            Dim dataStream As Stream = webreq.GetRequestStream()
            dataStream.Write(byteArray, 0, byteArray.Length)
            dataStream.Close()

            Dim res As WebResponse = webreq.GetResponse()
            Dim stream As Stream = res.GetResponseStream()
            Dim streamReader As New StreamReader(stream)
            str = streamReader.ReadToEnd
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Return str.ToString
    End Function


Is This A Good Question/Topic? 0
  • +

Replies To: REST Api Call

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5186
  • View blog
  • Posts: 13,916
  • Joined: 18-April 07

Re: REST Api Call

Posted 16 November 2017 - 05:33 PM

Yup, keep in mind that REST calls are just your run of the mill HTTP style requests. The only thing you need to keep an eye on is the request types and if any headers need to be added. Usually with your REST API documentation they will mention a VERB like say "POST" or "GET" or "PUT" or "DELETE" etc. Just make sure that when you make the request that you send it with the appropriate verb. Otherwise you can use the standard HTTP client classes and methods for communicating with URLs.

:)
Was This Post Helpful? 0
  • +
  • -

#3 t3cho  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 227
  • Joined: 27-May 14

Re: REST Api Call

Posted 18 November 2017 - 11:29 AM

Thanks for your answer. But after few days im still struglling in circle.

Please can anyone give me a sample how to send this trhough visual basic .net

<form method="POST" action="https://creator.zoho.eu/api/sampleapps/json/sample/form/Employee/record/update">
<input type="hidden" name ="authtoken" value="**********">
<input type="hidden" name ="scope" id="scope" value="creatorapi">
<input type="text" name="criteria" value="Name=Gary">

<input type="text" name="Basic" value="20000">
<input type="text" name="Address" value="UK">

<input type="submit" value="Update Record">
</form>


Original link : ZOHO Update

My code for sending request..

Public Function getResponseFromUrl(ByVal url As String, ByVal params As String)
        Dim str As String = ""
        Try
            Dim webreq As HttpWebRequest = WebRequest.Create(url)
            webreq.Method = "POST"
            webreq.ContentType = "application/x-www-form-urlencoded"
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(params)
            Dim dataStream As Stream = webreq.GetRequestStream()
            dataStream.Write(byteArray, 0, byteArray.Length)
            dataStream.Close()

            Dim res As WebResponse = webreq.GetResponse()
            Dim stream As Stream = res.GetResponseStream()
            Dim streamReader As New StreamReader(stream)
            str = streamReader.ReadToEnd
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Return str.ToString
    End Function


just dont know how to pack this string :S

This post has been edited by t3cho: 18 November 2017 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1