Patch Request Android Volley
Asked Answered
H

1

10

I'm currently using Android's Volley networking library in a project I'm working on. I've pulled down the master branch of volley from https://android.googlesource.com/platform/frameworks/volley/, so my library project should be up to date, but only supports the following request methods:

/**
 * Supported request methods.
 */
public interface Method {
    int DEPRECATED_GET_OR_POST = -1;
    int GET = 0;
    int POST = 1;
    int PUT = 2;
    int DELETE = 3;
}

It probably wouldn't be much trouble to extend the library to support patch requests, so my question is why wouldn't patch requests be supported by the base library? Also, could anyone suggest any good git branches that have already added this support?

Hall answered 5/11, 2013 at 20:2 Comment(1)
Have you found an answer to this?Hangbird
H
12

I finally found an answer to this question. It is very stupid. The problem is not with the Volley framework. HTTPUrlConnection of Java does not support PATCH. There are way on the internet that uses Java Reflection to set the method object to PATCH but they brings additional problems.

I finally solved this problem using X-HTTP-Method-Override header. I made a normal POST request with body even and add this header like below.

X-HTTP-Method-Override: PATCH

and it worked. Your web server side should support method overriding though.

Hangbird answered 26/11, 2013 at 15:51 Comment(6)
Latest volley (since Nov 13, 2013) now supports PATCH method natively, just specify Method.PATCH for your request.Ultramarine
It is not working if you use HTTPUrlConnection with Volley. If you use Apache libraries with it, it works ok but if you use HTTPUrlConnection with it, it will fire an exception.Hangbird
So how do we fix the porblem @tasomaniac? Have found the solution yet? If so please, could you share here as I am facing the same problem.Ojeda
Anyone has an answer to this problem with Volley and PATCH? I feel like there's got to be a better, simple solution to this problem, but I can't find it anywhere. Any ideas?Ojeda
You can use Retrofit library from Square. If you must use Volley, we have 2 options. Either you use Apache's HTTPClient within Volley or you will use my method where I included this HTTP header to PATCH requests. The server has to support X-HTTP-Method-Override. Most servers support it.Hangbird
What a smart solution! Save my life!Haas

© 2022 - 2024 — McMap. All rights reserved.