PardonMyCheckmate
PardonMyCheckmate

Reputation: 87

.NET core API controller never recieves body from Angular client

I have an order API. Here I'm trying to add the order from the user so it takes in a full Order object. Here's the method in my controller:

 [HttpPost("addOrder")]
 public IActionResult addOrder([FromBody] Order order)

Here's my models @ server side:

public class Order
{
    public string OrderId { get; set; }
    public User User { get; set; }
    public double TotalOrder { get; set; }
    public IEnumerable<OrderItem> OrderItems { get; set; }
    public Order()
    {
        OrderItems = new List<OrderItem>();
    }
}

public class OrderItem
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string ProductName { get; set; }
    public double Price { get; set; }
    public int Quantity { get; set; }
    [ForeignKey("CartId")]
    public Order Order { get; set; }
    public string OrderId { get; set; }
}

public class User
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string adress { get; set; }
}

When I try to test my method via Postman with the following body it reaches my API and the whole Order is included in my Order parameter:

{
    "OrderId": "OrderIDTest",
    "User": {
        "firstName": "FNT",
        "lastName": "LNT",
        "adress": "AT"
    },
    "TotalOrder": "999",
    "OrderItems": [
            {
                "ProductName": "Shoe 1",
                "Price": "99",
                "Quantity": 2
            },
             {
                "ProductName": "Shoe 2",
                "Price": "299",
                "Quantity": 4
            }
        ]
   }

but when I send it through my Angular, I never reaches my addOrder endpoints. Here's the object I'm sending printed out in a consolelog:

Object from Angular Client:

My Angular Service:

createOrder(order: Order) {
console.log(order);
let header = new HttpHeaders({'Content-Type': 'application/json'})
return this.HttpClient.post("https://localhost:44369/api/order/addOrder", order, { headers: header });

}

btw I know address is wrongly spelled

Upvotes: 0

Views: 42

Answers (1)

Zain Ahmad Khan
Zain Ahmad Khan

Reputation: 487

I think problem might be Request is not subscribed, this is one of the common issues. That's why request never goes out to the Api.

Please share the network request so i can better analyze.

Upvotes: 1

Related Questions