Create HTTP.PATCH request for firebase from JAVA Spring

The problem is that Java HTTPUrlConnection does not support HTTP.PATCH.

So, I found a guide that implements the delete implementation with the body. But I get HTTP/1.1 400 Bad Request

I came to 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 the Advanced Rest client, cannot understand why my Spring boot project does not get the same result, any help would be greatly appreciated.

0
source share
1 answer

There is nothing wrong with the patch instructions. Just my JSON was wrong. All thanks to @DaveSyer for making my narrow scope (focusing on the method, Patch red.) Expands.

Firebase wrote:

Error conditions

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

  • A request made via HTTP instead of HTTPS will result in HTTP404 status code not found

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

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

  • Attempting too much PUT or POST data causes HTTP status code 400 Bad Request

  • A REST API call that does not specify a namespace will result in an HTTP 417 Fusion Expectation Failed status code

  • REST API call containing invalid child names as part of an invalid path400 request

  • A request that violates security rules will result in an HTTP status code of 403 Forbidden

+1
source

Source: https://habr.com/ru/post/973524/


All Articles