简体   繁体   English

如何使用AsyncHttpClient使用JSON编码主体构建HTTP DELETE请求

[英]How to build HTTP DELETE request with JSON encoded body using AsyncHttpClient

I need to write a HTTP client which to communicate with Floodlight OpenFlow controller via its REST API . 我需要编写一个HTTP客户端,以便通过其REST API与Floodlight OpenFlow控制器进行通信。

For testing I did it in python, and it worked OK. 为了进行测试,我使用python进行了测试,效果很好。 But now I'm in a situation where it has to be done in Java, of which I'm admittedly still at the beginner's level. 但是现在我必须要用Java来完成它,但我仍然处于初学者水平。 One of my apps uses AsyncHttpClient to dispatch async GET requests, and works just fine. 我的一个应用程序使用AsyncHttpClient调度异步GET请求,并且工作正常。 Now as a Floodlight's REST client, it has to do POST and DELETE with JSON encoded body. 现在,作为Floodlight的REST客户端,它必须使用JSON编码的主体进行POST和DELETE。 My code for an async POST request works very much as expected. 我的异步POST请求代码正常工作。

But no luck with DELETE. 但是DELETE运气不好。

Somehow it doesn't write JSON string into its request body. 不知何故,它没有将JSON字符串写入其请求主体。 The code is almost identical with POST. 该代码与POST几乎相同。 For debugging, I don't feed an AsyncCompletionHandler instance to execute() method. 对于调试,我不将AsyncCompletionHandler实例提供给execute()方法。

System.out.println(ofEntry.toJson());  // this returns {"name": "xyz"} as expected.

Future<Response> f = httpClient.prepareRequest(new RequestBuilder("DELETE")
                                     .setUrl("http://" + myControllerBaseUrl + urlPathFlowPostDelete)
                                     .setHeader("content-type", "application/json")
                                     .setBody(ofEntry.toJson())
                                     .build()).execute();

System.out.println(f.getStatusCode());  // returns 200.

System.out.println(f.getResponseBody()); // returns {"status" : "Error! No data posted."}.

Just to make sure, I peeped into packet dump with wireshark, and found out the server isn't lying :) 为了确保这一点,我偷窥了wireshark的数据包转储,发现服务器没有在撒谎:)

The author of the library has written an extensive amount of relevant, valuable information, but unfortunately I can't find example code specifically for building a DELETE request. 该库的作者已经编写了大量相关的,有价值的信息,但是不幸的是,我找不到专门用于构建DELETE请求的示例代码。

I'd very much appreciate any suggestions, pointers, and of course pinpoint solutions! 我非常感谢您的任何建议,指点,当然还有精确的解决方案!

Not sure that replying to my own question is appropriate here, but I have just found a related thread at the floodlight-dev Google group. 不确定在这里回答我自己的问题是否合适,但是我刚刚在Floodlight-dev Google小组中找到了一个相关的主题。

Problem with Static Flow Pusher DELETE REST method 静态流程推送器DELETE REST方法的问题

So this could be a problem with Floodlight REST API which requires message body for a DELETE request to identify what to be deleted, whereas AHC is simply compliant with RFC2616. 因此,这可能是Floodlight REST API的问题,该API要求消息主体用于DELETE请求以标识要删除的内容,而AHC仅符合RFC2616。

I will follow the thread at Google group, and see how it will conclude among developers. 我将关注Google小组的主题,并在开发人员中探讨它的结局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM