Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to PATCH data using System.Net.Http

I have uploaded a file to SharePoint and found out what id it has. Now I need to update some of the other columns on that listitem. The problem is that System.Net.Http.HttpMethod.Patch doesn't exist.

public static async Task<string> UpdateFileData()
{
    var (authResult, message) = await Authentication.AquireTokenAsync();

    string updateurl = MainPage.rooturl + "lists/edd49389-7edb-41db-80bd-c8493234eafa/items/" + fileID + "/";
    var httpClient = new HttpClient();
    HttpResponseMessage response;
    try
    {
        var root = new
        {
            fields = new Dictionary<string, string>
            {
                { "IBX", App.IBX },  //column to update
                { "Year", App.Year}, //column to update
                { "Month", App.Month} //column to update
            }
        };

        var s = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
        var content = JsonConvert.SerializeObject(root, s);
        var request = new HttpRequestMessage(HttpMethod.Put, updateurl);
        request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
        request.Content = new StringContent(content, Encoding.UTF8, "application/json");
        response = await httpClient.SendAsync(request);
        var responseString = await response.Content.ReadAsStringAsync();
        return responseString;
    }
    catch (Exception ex)
    {
        return ex.ToString();
    }
}
like image 636
CET Avatar asked Dec 30 '18 23:12

CET


People also ask

What is http patch method?

In computing, the PATCH method is a request method in HTTP for making partial changes to an existing resource. The PATCH method provides an entity containing a list of changes to be applied to the resource requested using the HTTP Uniform Resource Identifier (URI).

What is http patch C#?

HTTP POST can support partial updates to a resource. But there is a separate PATCH method. This new HTTP method, PATCH, modifies an existing HTTP resource. The call to the Patch method is sufficient to partially update the corresponding properties of the Employee object in the list.

Why PATCH is used in HTTP?

The HTTP Patch method is used to request a set of modifications in the request entity to be applied for the resource recognized by the Request-URI. This method plays a vital role in improving interoperability and preventing errors by making partial changes in the resource.

How do I send a PATCH request?

To send a PATCH request to the server, you need to use the HTTP PATCH method and include the request data in the body of the HTTP message. The Content-Type request header must indicate the data type in the body. In this PATCH request example, we send JSON to the ReqBin echo endpoint to update the data on the server.


1 Answers

Can't you just use the HttpMethod class constructor?

new HttpMethod("PATCH");

Source: https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpmethod.-ctor?view=netframework-4.7.2#System_Net_Http_HttpMethod__ctor_System_String_

like image 131
Szab Avatar answered Oct 28 '22 23:10

Szab