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

1.7k Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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