using HttpMethod Patch in .NET Framework

1.1k Views Asked by At

I need to use Patch method in my Web API. I tried like this:

using (var request = new HttpRequestMessage(new HttpMethod("Patch"), new Uri(url)))
{
    var plainTextBytes = System.Text.Encoding.UTF8.GetBytes("log:pass");
    string val = System.Convert.ToBase64String(plainTextBytes);
    request.Headers.Add("Authorization", "Basic Auth "+val);
}

Because HttpMethod.Patch works in .NET Core, but I still get a response of "wrong method".

I saw all posts about this, but I did not get answer for my question

I am using .NET FRAMEWORK and there is no HttpMethod.Patch but in Postman i have responce 200 OK. Now i have an idea i have problem with sending body or the method.

I am defining method using:

request.Method = new HttpMethod("Patch");

But content i am defining using:

var jsonString = new StringContent(JsonConvert.SerializeObject(link), Encoding.UTF8,"application/json");
                            HttpContent content = jsonString;
                            request.Content = content;
                            var patch2Result = client.SendAsync(request);
1

There are 1 best solutions below

0
heeen On

The HTTP verb should be in all caps

request.Method = new HttpMethod("PATCH");

or try using

request.Method = HttpMethod.Patch; // not caps because it is the name of the property, not the verbatim the verb that goes in the request