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.” }
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
103 Points
793 Posts
Submit data to designated API endpoints.
Jul 06, 2020 08:53 AM|kengkit|LINK
Hi guys.. can help me to see what's wrong to my code?
i'm not able to submit data to designated API endpoints.
Parameters: (guideline)
Contributor
2870 Points
843 Posts
Re: Submit data to designated API endpoints.
Jul 07, 2020 03:30 AM|Sean Fang|LINK
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:
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):
Hope this can help you.
Best regards,
Sean