Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello everyone,
I am new to web API and i need to implement a call back url for a web service.I am able to post a request to the uri and receive a json response from the post.

I am expected to implement a callback to receive the status of my transaction in order to update my transaction accordingly. I have been on this for days now as i am unable to get the values posted to my callback function.

Here are my codes. Please help me, what am i doing wrong?

What I have tried:

Here is my class
<pre lang="VB">
Public Class MyCallback
        Public Property responseCode As String
        Public Property responseMessage As String
        Public Property TransactionId As String
        Public Property networkTransactionId As String
        Public Property merchantId As String
        Public Property productId As String
        Public Property refNo As String
        Public Property msisdn As String
        Public Property amount As String
        Public Property timestamp As String
        Public Property balance As String

    End Class


Then here is my implementation in StudentsController. I intend to get the values from the <frombody> attribute and use them implement my ADO.Net code.

VB
<pre>Public Function PostCallback(<FromBody> ByVal ITC As MyCallback) As HttpResponseMessage
            'Validate the reference if it's from our system (1=Yes,0=No)
            Dim Exists As Integer = ValidateTransactionNumber(ITC.refNo)
            If Exists = 0 Then
                Return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid reference")
            Else
                Try
                    Dim command As New SqlCommand("us_update_transaction_with_callback", connection)
                    command.CommandType = CommandType.StoredProcedure
                    command.Parameters.AddWithValue("@ResponseCode", Request.Content())
                    command.Parameters.AddWithValue("@ResponseMsg", ITC.responseMessage)
                    command.Parameters.AddWithValue("@APITransID", ITC.TransactionId)
                    command.Parameters.AddWithValue("@MerchantID", ITC.merchantId)
                    command.Parameters.AddWithValue("@NetworkTransID", ITC.networkTransactionId)
                    command.Parameters.AddWithValue("@PaymentCode", ITC.refNo)
                    command.Parameters.AddWithValue("@PhoneNumber", ITC.msisdn)
                    command.Parameters.AddWithValue("@Amount", ITC.amount)
                    command.Parameters.AddWithValue("@Timestamp", ITC.timestamp)
                    command.Parameters.AddWithValue("@Balance", ITC.balance)
                    If connection.State = ConnectionState.Closed Then
                        connection.Open()
                    End If
                    command.ExecuteNonQuery()
                    connection.Close()
                    Return Request.CreateResponse(HttpStatusCode.OK, "Callback completed successfully")
                Catch ex As Exception
                    Return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)
                End Try

            End If

        End Function


When i test in Postman, i get the following error
ASP.NET
{"Message":"No mapping exists from object type System.Web.Http.WebHost.HttpControllerHandler+LazyStreamContent to a known managed provider native type."}
Posted
Updated 8-Jul-21 5:51am

Quote:
VB.NET
command.Parameters.AddWithValue("@ResponseCode", Request.Content())
This line causes the error.

If you want to pass the full body content to the parameter, you will need to read it first - for example:
VB.NET
Dim body As String = Await Request.Content().ReadAsStringAsync()
command.Parameters.AddWithValue("@ResponseCode", body)

However, the parameter name suggests that you want to pass the response code from the parameter instead:
VB.NET
command.Parameters.AddWithValue("@ResponseCode", ITC.responseCode)
 
Share this answer
 
Thank you @Richard, exactly so, i would like to read and map the response code to the parameter. However,
Dim body As String = Await Request.Content().ReadAsStringAsync()
returns an empty string.
 
Share this answer
 
Hello team, i got it now.
There was a typo in the naming of the properties in the class. I got it now, thank you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900