Home > Software design >  How to make a POST request with postman?
How to make a POST request with postman?

Time:10-29

I have two models, Sever and Update. The ralationship between these two classes is 1...*N, meaning that each Server has multiple updates. Here is my Server model:

public class Server
    {
        public Server()
        {
            UpdateList = new HashSet<Update>();

        }
        [Key]
        [Required]
        public int ID { get; set; }
        [Required]
        public string ComputerName { get; set; }
        [Required]
        public string Type { get; set; }

        [Required]
        public string Estado { get; set; }

        [Required]
        public string Phase { get; set; }

        public virtual ICollection<Update> UpdateList { get; set; }
    }

Here is my Update model:

public class Update
    {
        [Required]
        public int ID { get; set; }

        public string updateId { get; set; }

        public string updateTitle { get; set; }

        public string updateDescription { get; set; }

        [System.ComponentModel.DataAnnotations.Schema.ForeignKey("Server")]
        [Display(Name = "Server")]
        public int? ServerFK { get; set; }
        public virtual Server Server { get; set; }
    }

Here is the POST request that I'm using in postman:

{
    "computerName":"ServerTestUpdate",
    "type":"ServidorAplicacional",
    "estado":"Reboot Pending",
    "phase":"0",
    "updateList":
        [{   
            
            "updateId":"idTest",
            "updateTitle":"Update1",
            "updateDescription":"TestDescription"
          
        }]
     

    }

However, when I do this, I get the following error:

System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.

Is there something wrong with the syntax or maybe is something in the controller?

Thank you in advance

CodePudding user response:

Most likely this is a problem in serializing the object, since deserialization wouldn’t care about cycles. It’s probably the fact that your Server object has a collection of Updates, all of which have a Server property pointing back and this causes a cycle.

If you add JsonIgnore attribute to the property this should go away (from System.Text.Json.Serialization namespace)

[JsonIgnore]
public virtual Server Server { get; set; }
  • Related