skip to Main Content

For referenece, I have attached the code for Customer Modal, Customer controller (Post request) and postman response,

Customer Controller: (post Req)

     [HttpPost]
     public async Task<ActionResult<Customer>> PostCustomer(Customer customer)
     {
          if (_context.Customers == null)
          {
              return Problem("Entity set 'SalesContext.Customers'  is null.");
          }

          _context.Customers.Add(customer);
          await _context.SaveChangesAsync();

          return CreatedAtAction("GetCustomer", new { id = customer.CustomerID }, customer);
    }

Customer model:

public class Customer
{
    public int CustomerID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
}
 

Postman :

Post request URL: https://localhost:44458/api/Customer_CRUD

Req. Body:

{
    "Name":"abc",
    "Address":"abc"
}

Response : 404 not found

404 not found coming in post request, should come 201

Response code should be 200 in place of 404, not able to identify mistake.

2

Answers


  1. You have to define the route attributes to change the default URL for your controller actions:

    [HttpPost]
    [Route("api/Customer_CRUD")]
    public async Task<ActionResult<Customer>> PostCustomer(Customer customer)
    {
        // your code
    }
    

    If you do not use route annotations in your controller, ASP.NET Core uses a default routing convention. The URL pattern for the default routing convention is {controller}/{action}/{id?}.

    Assuming the controller class name is CustomerController and the action method name is PostCustomer, you would make a POST request to the following URL:

    https://localhost:44458/Customer/PostCustomer
    
    Login or Signup to reply.
  2. Based on the information you provided, it seems like you are trying to send a POST request to an API endpoint for customer CRUD operations. However, without more information about the API endpoint code and configuration, it’s difficult to determine why you might be receiving a 404 error in Postman.

    Here are some possible reasons why you might be receiving a 404 error:

    Incorrect URL: Make sure that the URL you are using in Postman matches the URL of the API endpoint. Double-check that you are using the correct port number and path.

    Missing or incorrect route attribute: Make sure that the API endpoint is decorated with the correct route attribute. For example, if the route attribute is [Route("api/Customer_CRUD")], then the URL in Postman should be https://localhost:/api/Customer_CRUD.

    Authorization or authentication issues: If the API endpoint requires authentication or authorization, make sure that you are providing the correct credentials in Postman.

    Incorrect HTTP method: Make sure that you are using the correct HTTP method (POST) in Postman.

    API endpoint implementation issues: Make sure that the API endpoint code is correctly implemented and that it can handle the request body format and data.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search