Developer1903
Developer1903

Reputation: 127

VB.NET Receive Webhook

I have a system in VB.NET where I need to receive webhook from the financial system that generates accounts receivable and receives payments. All API integration is already working with this service, I just need to receive the webhook for each transaction status change to do the procedures in my database, because nowadays I check each transaction several times a day to see if my status has changed.

Their documentation says: "Transactional Webhook - This webhook is triggered and sends a POST notification for every change in the status of a transaction. It is applied to all forms of payment and comprises the entire life cycle of a transaction."

I never developed the receipt of webhooks and when researching about it, I had some doubts.

When integrating with the payments API, when creating a new charge for example, I send a POST to the API. When consulting a charge, I send a GET. All of this using WebRequest.

For Webhook, is it possible to do the same way, and instead of sending, receive POST on a page with WebRequest? Or to receive a webhook, the development is different?

This is an example of the code structure I use to send a POST:

Public Sub SendRequest(uri As Uri, jsonDataBytes As Byte(), contentType As String, method As String, requisicao As String)

    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12

    Dim request As WebRequest

    request = WebRequest.Create(uri)
    request.ContentLength = jsonDataBytes.Length
    request.ContentType = contentType
    request.Method = method
    request.Headers.Add("X-API-KEY", "xxxxxxx")

        Using requestStream = request.GetRequestStream

        requestStream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
        requestStream.Close()

        Try

            Using responseStream = request.GetResponse.GetResponseStream
                Using reader As New StreamReader(responseStream)

                    Dim objResponse As Object = reader.ReadToEnd()

                End Using
            End Using

        Catch ex As WebException
        End Try

    End Using

End Sub

Upvotes: 1

Views: 1742

Answers (1)

SteveCinq
SteveCinq

Reputation: 1963

A webhook sends a request, usually POST but sometimes GET or OPTION for validation, to your web server which your code handles and responds to (or, often, responds to before handling due to time-outs on the sending end).

You should always verify the request sender. This is often done using a request header identifier, such as a name or ID string.

Webhooks are sometimes described as "inverse" or "reverse" APIs since you are not making requests ("polling") but reacting to requests from a third-party sending you information that you have subscribed to.

Upvotes: 2

Related Questions