I have some data in JSON format that I need to send to a web address via a POST request. The data resembles the following example:
[
{
"Id":0,
"Name":"John"
},
{
"Id":0,
"Name": "James"
}
]
I would normally have no problem uploading the data as a NameValueCollection
using the WebClient.UploadValues()
method:
using (WebClient Client = InitWebClient()) // InitWebClient() initialises a new WebClient and sets the BaseAddress and Encoding properties
{
NameValueCollection Values = new NameValueCollection
{
{"Id", "0"},
{"Name", "John"}
};
Client.UploadValues("/api/example", "POST", Values);
}
However as this JSON request is a list (and must always be in the form of a list even if there is only one item being uploaded) I am at a loss as to how this is done.
Due to programming constraints I need to do all of this using the WebClient object.