4.6Adding JSON content
When you need to add JSON data to multipart form content, HttpMultipartFormDataBuilder provides flexible handling depending on whether a form name is specified.
1. No form name specified: In this case, the JSON data is parsed and traversed, and its properties are set as individual form items. Whether you pass an anonymous type or a JSON string, the result is the same.
HttpRequestBuilder.Post("https://furion.net") .SetMultipartContent(multipart => { multipart.AddJson(new { id = 1, name = "furion" }); // No form name specified (will be assigned to the Id and Name properties of FormClass) // multipart.AddJson("{\"id\":1,\"name\":\"furion\"}"); // Supports JSON strings. Same as above. });The above code will generate two form items: Id and Name, which correspond to the class definition received on the server side, such as:
public class FormClass{ public int Id { get; set; } public string Name { get; set; } // other properties}2. Form name specified: If a form name is specified for the JSON data, the entire JSON object is set as a nested item of the form. This is typically used when the server expects to receive an object with a nested structure.
HttpRequestBuilder.Post("https://furion.net") .SetMultipartContent(multipart => { multipart.AddJson(new { id = 1, name = "furion" }); // No form name specified (will be assigned to the Id and Name properties of FormClass) multipart.AddJson(new { id = 1, name = "furion" }, "child"); // Form name specified, will be assigned to the Child property of FormClass // multipart.AddJson("{\"id\":1,\"name\":\"furion\"}", "child"); // Supports JSON strings. Same as above. });In this case, the class definition received on the server side should include a nested class, such as:
public class FormClass{ public int Id { get; set; } public string Name { get; set; } public ChildClass Child { get; set; } // nested class // other properties...}public class ChildClass{ public int Id { get; set; } public string Name { get; set; } // other properties...}