1

I'm using a REST full API, which needs a PATCH header... So in xamarin, you only have getasync, postasync, putasync and send async...

But I have no clue on how to add /make a PATCH async method (or even do such a request)

This is the code I currently have to be send via a patch

Folder add_list_to_folder = new Folder()
                    {
                        // revision is required, so we take the current revision and add one to it.
                        revision = folder_to_be_updated.revision += 1,
                        list_ids = arr_of_lists
                    };

Now, I think that needs to get seialized, so like this:

response = await client.PostAsync(url,
                    new StringContent(
                        JsonConvert.SerializeObject(add_list_to_folder),
                        Encoding.UTF8, "application/json"));

But that is a post, so how can I make an PATCH request?

0

1 Answer 1

2

You should be able to send a completely custom request with the HttpClient. Since PATCH isn't a very common verb, there isn't a shorthand for it.

From the top of my head, try something like this:

var method = new HttpMethod("PATCH");

var request = new HttpRequestMessage(method, url) {
    Content = new StringContent(
                    JsonConvert.SerializeObject(add_list_to_folder),
                    Encoding.UTF8, "application/json")
};

var response = await client.SendAsync(request);

If you have to use it at several places, it might be just as neat to wrap it in an extension method, like:

public static async Task<HttpResponseMessage> PatchAsync(this HttpClient client, Uri requestUri, HttpContent httpContent)
{
    // TODO add some error handling
    var method = new HttpMethod("PATCH");

    var request = new HttpRequestMessage(method, requestUri) {
        Content = httpContent
    };

    return await client.SendAsync(request);
}

Now you should be able to call it directly on your HttpClient like the post or get methods, i.e.:

var client = new HttpClient();
client.PatchAsync(url, new StringContent(
                    JsonConvert.SerializeObject(add_list_to_folder),
                    Encoding.UTF8, "application/json"));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.