如何为Http Delete方法设置RequestBody。 [英] How to set RequestBody for Http Delete method.

查看:591
本文介绍了如何为Http Delete方法设置RequestBody。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个删除API的服务器的客户端代码。 API规范要求发送数据。我正在使用HttpComponents v3.1库来编写客户端代码。使用 HtpDelete 类我找不到向它添加请求数据的方法。有办法吗?下面是代码片段。

I am writing a client code for a server which Delete API. The API specification requires data to be sent. I am using HttpComponents v3.1 library for writing client code. Using the HtpDelete class I could not find a way to add request data to it. Is there a way to do so ? Below is the code snippet.

        HttpDelete deleteReq = new HttpDelete(uriBuilder.toString());
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new BasicNameValuePair(RestConstants.POST_DATA_PARAM_NAME, 
            postData.toString()));
    try {
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams);
        entity.setContentEncoding(HTTP.UTF_8);
        //deleteReq.setEntity(entity); // There is no method setEntity()
        deleteReq.setHeader(RestConstants.CONTENT_TYPE_HEADER, RestConstants.CONTENT_TYPE_HEADER_VAL);
    } catch (UnsupportedEncodingException e) {
        logger.error("UnsupportedEncodingException: " + e);
    }

提前致谢。

推荐答案

我没有试过这个,它就像地狱一样hackish,如果事实证明有更好的解决方案,我会感到更快乐,但你可能会尝试扩展PostMetod并覆盖getName()方法返回DELETE。

I haven't tried this, and it is as hackish as hell and I wolud feel happier if it turns out there is better solution, but you might try to extend PostMetod and override getName() method to return "DELETE".

这篇关于如何为Http Delete方法设置RequestBody。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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