How to invoke POST method by adding [FromBody] attribute in Postman tool
-
I have a POST method with [FromBody] attribute. I tried to call the same from PostMan. But everytime Postman gives 404 error. The Web Api is not hitting at all. Following is my source code:
[HttpPost]
[Route("api/CustomerService")]
public HttpResponseMessage GetServiceChats([FromBody]string to, string from)
{ }What I have done is, I added the parameters by Selecting Body-->raw-->JSON options and type the parameters as below:
{
"to" : "919191919191",
"from" : "90909090900"
}What could be the probable reason? If the same I attached in URL without the FromBody attribute, it works. Please provide any suggestions.
-
I have a POST method with [FromBody] attribute. I tried to call the same from PostMan. But everytime Postman gives 404 error. The Web Api is not hitting at all. Following is my source code:
[HttpPost]
[Route("api/CustomerService")]
public HttpResponseMessage GetServiceChats([FromBody]string to, string from)
{ }What I have done is, I added the parameters by Selecting Body-->raw-->JSON options and type the parameters as below:
{
"to" : "919191919191",
"from" : "90909090900"
}What could be the probable reason? If the same I attached in URL without the FromBody attribute, it works. Please provide any suggestions.
The
[FromBody]
attribute is used to read a single simple value from the request body: Parameter Binding in ASP.NET Web API - ASP.NET 4.x | Microsoft Docs[^] Given your signature, thefrom
parameter needs to be a query-string parameter, and the request body needs to be simply:"90909090900"
If you want the API to match the request you've shown, use a model to represent the parameters:
public class ServiceChatsModel
{
public string To { get; set; }
public string From { get; set; }
}[HttpPost]
[Route("api/CustomerService")]
public HttpResponseMessage GetServiceChats(ServiceChatsModel model)
{
...
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
The
[FromBody]
attribute is used to read a single simple value from the request body: Parameter Binding in ASP.NET Web API - ASP.NET 4.x | Microsoft Docs[^] Given your signature, thefrom
parameter needs to be a query-string parameter, and the request body needs to be simply:"90909090900"
If you want the API to match the request you've shown, use a model to represent the parameters:
public class ServiceChatsModel
{
public string To { get; set; }
public string From { get; set; }
}[HttpPost]
[Route("api/CustomerService")]
public HttpResponseMessage GetServiceChats(ServiceChatsModel model)
{
...
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
The
[FromBody]
attribute is used to read a single simple value from the request body: Parameter Binding in ASP.NET Web API - ASP.NET 4.x | Microsoft Docs[^] Given your signature, thefrom
parameter needs to be a query-string parameter, and the request body needs to be simply:"90909090900"
If you want the API to match the request you've shown, use a model to represent the parameters:
public class ServiceChatsModel
{
public string To { get; set; }
public string From { get; set; }
}[HttpPost]
[Route("api/CustomerService")]
public HttpResponseMessage GetServiceChats(ServiceChatsModel model)
{
...
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks