Unable to Postman POST to dot net API when Foreign Keys are involved c#

I've made a simple warehouse management system and the site itself is running fine - but trying to do API tests with Postman POST's is not working specifically when trying to post a piece of Cargo who has two foreign keys that point to a customerID and a shipment ID.

My DB is made up of Customer, Shipment, & Cargo tables at the moment.

I can POST an actual Shipment entry via Postman as it has no foreign keys. But when I attempt to post a piece of cargo which has foreign keys of Shipment & Customer I keep being told by Postman "405 method not found"

I've created -API controllers for this specific purpose as well. And the Shipment is working just fine.

Here are two methods in the APICargoSend controller I've tried to build doing the same thing for testing purposes.

    [HttpPost]
    [AllowAnonymous]
    public async Task<ActionResult<Cargo>> PostCargo(Cargo cargo)
    {
        _context.Cargo.Add (cargo);
        await _context.SaveChangesAsync();

        return CreatedAtAction("GetCargo", new { id = cargo.Id }, cargo);
    }

    [HttpPost] // post method
    [AllowAnonymous]
    public async Task<IActionResult>
    CargoPost(
        [
            Bind(
                "Id,TypeOfPallet,Length,Width,Height,Weight,Damage,Shipment,Customer")
        ]
        Cargo cargo
    )
    {
        if (ModelState.IsValid)
        {
            _context.Cargo.Add (cargo);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetCargo",
            new { id = cargo.Id },
            cargo);
        }
        return Ok("Succeeded");
    }

This is my cargo db model

    {public int Id { get; set; }
    [DisplayName("Type of Pallet")]
    public PalletType TypeOfPallet { get; set; }
    public double Length { get; set; }
    public double Width { get; set; }
    public double Height { get; set; }
    public double Weight { get; set; }
    public bool Damage { get; set; }
    public Shipment Shipment { get; set; }
    public Customer Customer { get; set; }
}

public enum PalletType
{
    HeatTreated,
    Wooden,
    Metal,
    Tote,
    Crate,
    SkeletonCrate,
    NonHeatTreated,
    EuropeanPallet
}

And here is my POSTMAN code - I've tried a variety of different ways all to no avail.

"Id": 20,
"TypeOfPallet": 0,
"Length": 20,
"Width": 40,
"Height": 50,
"Weight": 120,
"Damage": true,
"foreign_key": {
    "Shipment": 1,
    "Customer": 2
}
} 

I've also done

{
"Id": 20,
"TypeOfPallet": 0,
"Length": 20,
"Width": 40,
"Height": 50,
"Weight": 120,
"Damage": true,
"Shipment: 1,
"Customer: 2
}

Unable to even hit a breakpoint when debugging and I'm seriously lost.



Read more here: https://stackoverflow.com/questions/67012621/unable-to-postman-post-to-dot-net-api-when-foreign-keys-are-involved-c-sharp

Content Attribution

This content was originally published by Zefiron at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: