Home > front end >  HttpPost returns 404 if Content-Type is set to application/json - .netcore6 api
HttpPost returns 404 if Content-Type is set to application/json - .netcore6 api

Time:08-19

I am trying to write an API to handle webhook posts from Clover and their header specifies content-Type: application/json

for some reason my HttpPost method is rejecting the post with a 404 error (I assume it's my method) I can post to my endpoint no problem with Postman as long as my content-type is not application/json - as soon as I switch to that I get 404 as well

I'm missing something basic here :/ any ideas?

If I post (using Reqbin) my end point accepts and returns 200 OK

POST /auctionapi/Auction HTTP/1.1
Host: www.someweb.com
Content-Length: 60

{"verificationCode": "b860be7e-6ac4-4b56-8ac6-f44cf238a296"}

and if I change the content-type I get 404...

POST /auctionapi/Auction HTTP/1.1
Host: www.someweb.com
Content-Type: application/json
Content-Length: 60

{"verificationCode": "b860be7e-6ac4-4b56-8ac6-f44cf238a296"}

My code...

[Route("[controller]")]
[ApiController]
public class AuctionController : ControllerBase
{
    private readonly PCSOAuctionsContext _context;
    public AuctionController(PCSOAuctionsContext context)
    {
        _context = context;
    }      

    [HttpPost("receive")]
    public async Task<IActionResult> receive()
    {
        return StatusCode(200, "Thanks for using the API");
    }
}

CodePudding user response:

Try doing something like this and see if that resolve the 404. The HttpPost is being sent content type of json, but your method is not setup to receive it, therefore it will 404 because a proper route is not being found.

[Route("pcsoauctionapi/[controller]")]
[ApiController]
public class AuctionController : ControllerBase
{
    private readonly PCSOAuctionsContext _context;
    public AuctionController(PCSOAuctionsContext context)
    {
        _context = context;
    }

    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "PCSO Auction API", "Online" };
    }      

    [HttpPost("receive")]
    public async Task<IActionResult> receive([FromBody] object jsonData)
    {
        return StatusCode(200, "Thanks for using the API");
    }
}

I just tested my above method and it works just fine. So something is wrong with your Clover request. Here is result from swagger.

enter image description here

It shouldn't make a difference but don't set the content length and try sending then. Also can you post the CURL request clover is making. That will definitely reveal where issue is. here is my curl request.

curl -X 'POST' \
  'https://localhost:7777/api/V1/receive' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -d '{"verificationCode": "b860be7e-6ac4-4b56-8ac6-f44cf238a296"}'
  • Related