1

I am currently developing a REST-ful C# application that works with an external service. I have gotten basic 'GET', 'POST', 'PUT', and 'DELETE' requests to work, however whenever I attempt to send a PATCH request to the same service, it does not work. I get a 200 message (meaning it was successful), but doing a GET on the subject that I am trying to attempt the PATCH request on shows that no change occurred.

Additionally, whenever I use Fiddler to form PATCH requests, using the same URL and content string, it works as expected.

I am attempting to ensure that it isn't something I am doing wrong in regards to how I am sending the HTTP request.

My call that interacts with all of the different above REST functions is as follows:

    public async Task<T> GetResultFromService<T>(string requesttype, string url, string request = "")
    {
        HttpRequestMessage message;
        switch (requesttype)
        {
            case "Post":
                message = PostRequest(url, client, request);
                break;
            case "Put":
                message = PutRequest(url, client, request);
                break;
            case "Patch":
                message = PatchRequest(url, client, request);
                break;
            case "Delete":
                message = DeleteRequest(url, client);
                break;
            default:
                message = GetRequest(url, client);
                break;
        }

        var response = await client.SendAsync(message);
        var responseString = await response.Content.ReadAsStringAsync();
        return IModel.FromJson<T>(responseString);
    }

My function for forming the PATCH request:

    public HttpRequestMessage PatchRequest(string url, HttpClient client, string content = "")
    {
        var request = new HttpRequestMessage(new HttpMethod("PATCH"), url);

        return request;
    }

Adding a GET request for comparison:

    public HttpRequestMessage GetRequest(string url, HttpClient client)
    {
        var request = new HttpRequestMessage(HttpMethod.Get, url);

        return request;
    }

And my Httpclient code (if it is at all relevant...):

        var proxy = new WebProxy
        {
            Address = ServerEnv.ProxyUri
        };
        var httpHandler = new HttpClientHandler
        {
            Proxy = proxy,
            UseProxy = true
        };

        client = new HttpClient(httpHandler, true);

Any sort of direction you can point me in would be greatly appreciated.

Thanks a ton!

edit: I realized my mistake due to the answerer's reply. Basically I accidentally left out the body on my PATCH, but it was on my POST and PUT and I had overlooked that detail. POST code for detail:

    public HttpRequestMessage PostRequest(string url, HttpClient client, string content)
    {
        var request = new HttpRequestMessage(HttpMethod.Post, url)
        {
            Content = new StringContent(content,
                Encoding.UTF8,
                "application/json")
        };

        return request;
    }

and the revised PATCH:

    public HttpRequestMessage PatchRequest(string url, HttpClient client, string content = "")
    {
        var request = new HttpRequestMessage(new HttpMethod("PATCH"), url)
        {
            Content = new StringContent(content,
                Encoding.UTF8,
                "application/json")
        };

        return request;
    }

If any lesson can be learned from this, it would be that you should double and triple check copy and pasted code. Thanks again!

1 Answers1

2

Your POST, PUT and PATCH requests are missing a body. How should the service handing the request know what you want to change on the resource? You need to set the Content property on the HttpRequestMessage.

See this answer for an example.

MvdD
  • 22,082
  • 8
  • 65
  • 93