locked
Submit data to designated API endpoints. RRS feed

  • Question

  • User944339287 posted

    Hi guys.. can help me to see what's wrong to my code?

    i'm not able to submit data to designated API endpoints.

        Private Function registration() As Boolean
    
            Dim serviceUrl As String = "https://mydomain.com/api/registration"
            Dim input As Object = New With { _
                            .APIKey = "b7f3ac51f3a9c-043ed53-4453e0-873-2c151f0cf", _
                            .FullName = "Tony Stark", _
                            .Username = "tonystark@gmail.com", _
                            .Password = "qwert123" _
            }
    
            Dim inputJson As String = (New JavaScriptSerializer()).Serialize(input)
    
            Dim httpRequest As HttpWebRequest = DirectCast(WebRequest.Create(New Uri(serviceUrl)), HttpWebRequest)
            httpRequest.Accept = "application/json"
            httpRequest.ContentType = "application/json"
            httpRequest.Method = "POST"
    
            Dim bytes As Byte() = Encoding.UTF8.GetBytes(inputJson)
    
            Using stream As Stream = httpRequest.GetRequestStream()
                stream.Write(bytes, 0, bytes.Length)
                stream.Close()
            End Using
    
            Using httpResponse As HttpWebResponse = DirectCast(httpRequest.GetResponse(), HttpWebResponse)
                Using stream As Stream = httpResponse.GetResponseStream()
                    Me.lbl_result.Text = (New StreamReader(stream)).ReadToEnd()
                End Using
            End Using
    
        End Function

    Parameters: (guideline)

    JSON format for input:
    {
    “APIKey”: ”b7f3ac51f3a9c-043ed53-4453e0-873-2c151f0cf”
    “FullName”: ”Tony Stark”
    “Username”: “tonystark@gmail.com”
    “Password”: ”qwert123”
    }
    API Responses:
     
    Success: { ResponseCode: 1, Status: “Success”, Message: “Account has been created successfully!” } 
    
    Error : { ResponseCode: 0, Status: “Failed”, Message: “Registration Failed.” }




    Monday, July 6, 2020 8:53 AM

Answers

  • User-1330468790 posted

    Hi kengkit,

    Basically, your codes are correct and should be working provided you have configured the WebApiConfig.cs correctly firstly. In another word, the problem might locate in Route Configuration.

    I can see that in the previous version of your description is that you have used the route "https://mydomain.com/api/registration/GetData". This requires you configure a route to map "api/{controller}/{action}". However, the default route is "api/{controller}/{id}" which does not contain the route parameter "{action}".

    You might need to add a route as below to reach the action:

    config.Routes.MapHttpRoute(name:="ActionApi", routeTemplate:="api/{controller}/{action}/{id}", defaults:=New With {Key
        .id = RouteParameter.[Optional]
    })

    Moreover, the action should be set as following format (below is just a simple example so that you have to write your own action to fit your scenario):

    <HttpPost>
        Public Function GetData(
        <FromBody> ByVal user As User) As String
            Dim user1 As User = user
            Return JsonConvert.SerializeObject(user1)
        End Function
    
        Public Class User
            Public Property APIKey As String
            Public Property FullName As String
            Public Property Username As String
            Public Property Password As String
        End Class

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 7, 2020 3:30 AM