从JAVA Spring创建HTTP.PATCH请求到firebase [英] Create HTTP.PATCH request to firebase from JAVA Spring

查看:786
本文介绍了从JAVA Spring创建HTTP.PATCH请求到firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是Java HTTPUrlConnection 不支持HTTP.PATCH。

所以我找到了一个指南做了一个与身体删除的实现。但是我收到 HTTP / 1.1 400错误请求



b * 导入 compile(org.apache.httpcomponents:httpclient:4.3.1)



方法:

  @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);

字符串patchString ={'userwallposts':{'ame_lector_dk':[{'wallPostUrl':'/ wallposts / 1','createdByLink':'','+
''摘要':'blah gik sig ikke en tur,ogkøbteblah med hjem','createdDate':'20140126220550','comments':'2','+
''title':'1bla 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);






我检查了Url +方法在Advanced Rest客户端,不知道为什么我的Spring启动项目没有得到相同的输出,任何帮助将不胜感激。

解决方案

补丁语句没有错。这只是我的JSON是不正确的。
感谢@DaveSyer让我的视线狭窄(侧重于方法,补丁红色)展开。


Firebase写道:



错误情况


  • Firebase REST API将会返回错误代码在这些情况下。

  • 通过HTTP而不是HTTPS进行的请求将导致HTTP状态代码404未找到


  • 无法解析PUT或POST数据会导致HTTP状态码400错误请求

    >缺少PUT或POST数据将导致HTTP状态码400错误请求
  • 试图对PUT或POST数据过大导致HTTP状态码400错误请求 没有指定名称空间的REST API调用将导致HTTP状态代码417期望失败


    包含无效子名称作为path400 Bad Request的一部分的REST API调用
    li>

    违反安全规则的请求将导致HTTP状态代码403禁止



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.

解决方案

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

这篇关于从JAVA Spring创建HTTP.PATCH请求到firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆