Home > other >  Unable to invoke [HttpPost] web api
Unable to invoke [HttpPost] web api

Time:01-12

Below is my post method

[HttpPost]
public string Happay(string fileName) 
{
    //some code
}

When I try to invoke it from postman using this URL https://localhost:44313/SAPAPI/Happay?fileName=Payload1 it works but when I try to do the same from the browser it give me an exception that says

Resource not found

I removed the [HttpPost] attribute from the top an then I was able to invoke the method from the browser.

CodePudding user response:

The QueryString (in your case ?fileName=Payload1) is only applicable for GET requests.

In case of POST you can provide parameters

  • as part of the request route, like POST /SAPAPI/Happay/Payload1
  • as a request body, like filename=Payload1 as raw
  • as a request header, like "x-file-name": "Payload1"

Depending on the mode you need to specify from where do you expect the parameter

  • public string Happay([FromRoute]string fileName)
  • public string Happay([FromBody]string fileName)
  • public string Happay([FromHeader(Name = "x-file-name")]string fileName)

Parameter as Sample Code change
Route POST /SAPAPI/Happay/Payload1 [FromRoute(Name="fileName")]string fileName
Body fileName=Payload1 [FromBody(Name="fileName")]string fileName
Header "x-file-name": "Payload1" [FromHeader(Name="x-file-name")]string fileName

CodePudding user response:

Add [FromBody] before the parameter.

[HttpPost]
public string Happay([FromBody] string fileName) 
{
    //some code
}

CodePudding user response:

If you are calling the url from a browser, this is a GET request. Please learn more about REST.

  • Related