我正在使用REST完整API,它需要一个PATCH标头......所以在xamarin中,你只有getasync,postasync,putasync和send async ...
但我不知道如何添加/制作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
};
现在,我认为需要进行干预,所以就像这样:
response = await client.PostAsync(url,
new StringContent(
JsonConvert.SerializeObject(add_list_to_folder),
Encoding.UTF8, "application/json"));
但这是一个帖子,那么如何发出PATCH请求呢?
答案 0 :(得分:1)
您应该能够使用HttpClient
发送完全自定义的请求。由于PATCH
不是一个非常常见的动词,因此没有简写。
从我的头脑中,尝试这样的事情:
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);
如果你必须在几个地方使用它,将它包装在扩展方法中可能同样简洁,例如:
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);
}
现在您应该可以直接在HttpClient
上像帖子或获取方法一样调用它,即:
var client = new HttpClient();
client.PatchAsync(url, new StringContent(
JsonConvert.SerializeObject(add_list_to_folder),
Encoding.UTF8, "application/json"));