Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Web API code for processing a POST request

$
0
0

What is the server-side code required to process the following web api POST request?

       Private Function Authorise(authdata As authdata) As String

            Dim authtoken As String

            Dim payload As String = New JavaScriptSerializer().Serialize(authdata)

            ' Create a byte array of the data to be sent

            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(payload)

            ' Setup the Request

            Dim request As HttpWebRequest = CType(WebRequest.Create(App.DeploymentURL & "/api/v1/auth/"), HttpWebRequest)

            With request
                .Method = "POST"
                .UserAgent = "node-superagent/0.18.0"
                .ContentType = "application/json"
                .ContentLength = byteArray.Length
            End With

            ' Write data

            Dim postStream As System.IO.Stream = request.GetRequestStream()

            postStream.Write(byteArray, 0, byteArray.Length)
            postStream.Close()

            ' Send Request & Get Response

            Dim response As HttpWebResponse

            Try
                response = request.GetResponse()

                Dim j As Object

                Using reader As StreamReader = New StreamReader(response.GetResponseStream())
                    ' Get the Response Stream

                    Dim json As String = reader.ReadLine()

                    j = New JavaScriptSerializer().Deserialize(Of Object)(json)
                End Using

                If response.StatusCode = HttpStatusCode.OK Then
                    ' get and return the authentication token for the verify fucntion

                    authtoken = j("token")
                Else
                    Throw New trEx(eErrors.AUTH_ERROR, "Error getting authorisation for Toteraider - " & j("status"))
                End If

            Catch webex As WebException
                Throw New trEx(eErrors.WEBEXCEPTION, CType(webex.Response, HttpWebResponse).StatusCode & " " & CType(webex.Response, HttpWebResponse).StatusDescription & " when processing a Toteraider Auth" & vbCrLf & vbCrLf & "Request was: " & request.ToString & vbCrLf & vbCrLf & webex.ToString)
            End Try

            Return authtoken

        End Function

                       

Viewing all articles
Browse latest Browse all 4850

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>