0

Problem is that the Java HTTPUrlConnection does not support HTTP.PATCH.

So I found a guide that did an implementation of delete with with a body. But I'm receiving HTTP/1.1 400 Bad Request

I arrived at this:
* Import compile("org.apache.httpcomponents:httpclient:4.3.1")

Method:

@SneakyThrows
    public void firebasePatch(final PARAM p) {
        new TaskRunner() {

            @Override
            public void command() throws Throwable {
                final HttpClient httpClient = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 10000);
                PARAM p2 = p;
                String callUrl = url+".json"+"?auth="+firebaseAuth;
                HttpPatch httpPatch = new HttpPatch(callUrl);
                httpPatch.addHeader("Accept", "*/*");
                httpPatch.addHeader("Content-Type", "application/json");
//              httpPatch.addHeader("auth", firebaseAuth);

                String patchString = "{'userwallposts': {'ame_lector_dk': [{'wallPostUrl': '/wallposts/1','createdByLink': '',"+
                            "'summary': 'blah gik sig ikke en tur, og købte blah med hjem','createdDate': '20140126220550','comments': '2',"+
                            "'title': '1Blah blah blah','createdBy': 'PATCHED!','picture': 'http://images6.alphacoders.com/316/316963.jpg'}]}}";
                StringEntity entity = new StringEntity(patchString, "UTF-8");
                entity.setContentType("application/json");
                httpPatch.setEntity(entity);
                HttpResponse response = httpClient.execute(httpPatch);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == 200 ? true : false) {
                    // ok
                    System.out.println("patch complete");
                } else {
                    System.out.println(response.getStatusLine());
                }

            }
        }.run(nonBlock, exceptionHandling);

    }

I'v checked that the Url+method works in Advanced Rest client, can't figure out why my Spring boot project doesn't get the same output, any help will be greatly appreciated.

Anders Metnik
  • 6,096
  • 7
  • 40
  • 79
  • Could you simply use the Java SDK for Firebase? Would probably make things simpler if so. – Kato Mar 07 '14 at 17:10
  • Also, it looks like there is an HttpPatch: http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/methods/HttpPatch.html – Kato Mar 07 '14 at 22:40
  • What's the question exactly? What is it that doesn't work, and what are the symptoms? – Dave Syer Mar 08 '14 at 10:27
  • @Kato Because their "JAVA SDK" is actually an "Android SDK" which means it cant be run on not android devices. Also yes there is an HttpPatch, which I'm using in the above code snippet as well? – Anders Metnik Mar 10 '14 at 07:16
  • @DaveSyer I thought i had described it clearly. But when trying to access it the call goes through, but it gets 400 bad request. Even though it works from "Advanced Rest" (same url, same method, same body) – Anders Metnik Mar 10 '14 at 07:18
  • 1
    I guess you might need to ask the Firebase people what the 400 means. You might find it easier if your are using Spring to use RestTemplate instead of the raw `HttpClient`, but that doesn't explain the bad request. – Dave Syer Mar 10 '14 at 09:02
  • Yes @DaveSyer We have used RestTemplate for everything else, and it is a lot easier. Problem is that RestTemplate doesn't support the HTTP.Patch. I'll try and see if they have a solution. – Anders Metnik Mar 10 '14 at 09:36
  • `RestTemplate` supports PATCH since Spring 3.2 (but you have to use the right client library). – Dave Syer Mar 10 '14 at 10:06
  • True, but I couldn't figure out how to change the underlying client library in any ways. – Anders Metnik Mar 11 '14 at 07:37

1 Answers1

2

There is nothing wrong with the patch statement. It's simply that my JSON was incorrect. All thanks to @DaveSyer for making my narrow sight (focusing on the method, Patch red.) expand.

Firebase have written:

Error Conditions

  • The Firebase REST API will return error codes under these circumstances.

  • A request made over HTTP instead of HTTPS will result in an HTTP status code404 Not Found

  • Unable to parse PUT or POST data will result in an HTTP status code 400 Bad Request

  • Missing PUT or POST data will result in an HTTP status code 400 Bad Request

  • Attempting to PUT or POST data which is too large results in an HTTP status code 400 Bad Request

  • A REST API call that doesn't specify a namespace will result in an HTTP status code 417 Expectation Failed

  • A REST API call that contains invalid child names as part of the path400 Bad Request

  • A request that violates Security Rules will result in an HTTP status code 403 Forbidden

Anders Metnik
  • 6,096
  • 7
  • 40
  • 79