0

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);
  • 2
    Can you share more details of what issue you are facing? Share the Error message if there is any error. Does the API endpoint support the patch verb? – Chetan Jun 17 '22 at 04:19
  • It's `"Basic"` not `"Basic Auth"`. Also, there's a class for that `new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", val).ToString()`. Not a very useful class, but it does exist. – Jeremy Lakeman Jun 17 '22 at 04:29
  • Thx Jeremy i will look at It. Chetan Api support patch verb. Its only way to use what i need to. I tried with what i can use like Put or Post but i am not able – CallMeKacper Jun 17 '22 at 04:36
  • 1
    It would be difficult for you to post enough data for others to analyze, since such networking issues require a lot of data. But you might help yourself enough by learning tools that sneaks into .NET Core tracing on network activities, like https://github.com/lowleveldesign/dotnet-wtrace Besides, whenever post a question somewhere, people expect you post the exact error messages as well as other key details, not just "wrong method". – Lex Li Jun 17 '22 at 05:08
  • 1
    Use a tool like Fiddler to see the request both from your app and from Postman. Then you will se what the difference is. It may be a missing header. – Charlieface Jun 17 '22 at 12:12

1 Answers1

0

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
heeen
  • 4,736
  • 2
  • 21
  • 24