vb.netauthorize.netauthorize.net-webhooks

How to read data from body in Post method in VB.net


I want to create a post method to read data from body sent by Authorize.Net webhook using VB.Net and i'm new in VB.Net can someone please assist how to read data from body in a Post type method.


Solution

  • To send/receive HTTP requests/responses, typically you would use the HttpClient class (documentation). Specifically in this case you would:

    1. Call the PostAsync method (documentation) to submit the request
    2. Check the response's StatusCode (documentation) to verify that a successful response was returned (presumably an OK - 200 status)
    3. If a successful response was returned, get the response's Content (documentation)

    Here is a function (untested) that should get you going in the right direction:

    Imports System.Net
    Imports System.Net.Http
    Imports System.Threading.Tasks
    Imports Newtonsoft.Json
    Imports System.Text
    '...
    Private Async Function SendPost(url As String, data As Object) As Task(Of String)
        Dim body = String.Empty
        Using client = New HttpClient
            Dim content As New StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json")
            Dim response = Await client.PostAsync(url, content)
    
            If (response.StatusCode = HttpStatusCode.OK) Then
                body = Await response.Content.ReadAsStringAsync()
            End If
        End Using
        Return body
    End Function