Search code examples
c#arrayswindows-phone-8httpwebrequestput

Sending an array as part of a PUT method in C#


I am currently developing an app for Windows Phone 8 in C# that makes use of Quizlet.com's API, an online flashcard website.

One of the API calls documented in their API reference to edit a flashcard set requires you to send an array of terms and definitions through a PUT method.

Presumably do this you would have to use HttpWebRequest rather than WebClient, as the latter is too simple and doesn't support PUT requests to my knowledge. I cannot, however, seem to understand how you could send an entire array as part of a PUT request.

Does anyone have any ideas as to how one would do this? I'm sorry I don't have source code to attach, but it's probably not necessary as this is a more general question.

Thanks in advance!


This is the code I'm currently using that is not working:

RestClient Edit = new RestClient("https://api.quizlet.com");
            RestRequest EditRequest = new RestRequest();
            EditRequest.AddParameter("term_ids[]", ID);
            EditRequest.AddParameter("terms[]", Terms);
            EditRequest.AddParameter("definitions[]", Definitions);
            EditRequest.AddParameter("title", item.Title);
            EditRequest.AddHeader("Authorization", "Bearer " + CurrentLogin.AccessToken);
            EditRequest.AddHeader("Host", "api.quizlet.com");
            EditRequest.Resource = "2.0/sets/" + item.Id;
            EditRequest.Method= Method.PUT;
            Edit.ExecuteAsync(EditRequest, Response =>
            {
                FinalizeUpdate(Response);
            });

I declare my arrays as such:

    int[] ID;
    string[] Terms;
    string[] Definitions;

And I add data to my arrays as such (TermsList is an ObservableCollection):

        foreach(Term i in TermsList)
        {
            ID[Counter] = i.Id;
            Terms[Counter] = i.Name;
            Definitions[Counter] = i.Definition;
            Counter++;
        }

Below is the class definition for Term:

public class Term
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("term")]
    public string Name { get; set; }

    [JsonProperty("definition")]
    public string Definition { get; set; }

}

Solution

  • The API is RESTful, so I would recommend using an external library if possible, such as RestSharp.

    From what I can see in online examples of other APIs, the way to send an array is simply to send the same parameter again with a different value.

    i.e., using RestSharp's API you would do :

    client.AddParameter("imageData[]", data1);
    client.AddParameter("imageData[]", data2);