我想使用 HTTP DELETE 方法发送下面的 JSON 消息。 对于这个项目有必要使用OAuth2。所以我使用依赖项 google-oauth。对于每个 HTTP 请求,我使用依赖项 google 客户端。
{
"propertie" : true/false,
"members" : [
"String value is shown here"
]
}
在我的项目中,我使用了下面的代码,但我无法使用 HTTP DELETE 方法发送 JSON 消息。
Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(accessToken);
JsonArray members = new JsonArray();
JsonPrimitive element = new JsonPrimitive("String value is shown here");
members.add(element);
JsonObject closeGroupchat = new JsonObject();
closeGroupchat.addProperty("propertie", false);
closeGroupchat.add("members", members);
Gson gson = new Gson();
HttpContent hc = new ByteArrayContent("application/json", gson.toJson(closeGroupchat).getBytes());
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
HttpRequest httpreq = requestFactory.buildRequest(HttpMethods.DELETE, genericUrl, hc);
return httpreq.execute();
发生下一个错误:
java.lang.IllegalArgumentException: DELETE with non-zero content length is not supported
有人可以帮我解决这个问题吗?
最佳答案
您的问题是您的 HTTP DELETE 请求包含不应包含的正文。删除资源时,您需要提供要删除的 URL。 HTTP 请求中的任何正文都毫无意义,因为其目的是指示服务器删除特定 URL 处的资源。对于 GET 请求,经常会发生同样的事情 - 正文毫无意义,因为您正在尝试检索正文。
有关 HTTP DELETE 请求正文的详细信息,请参阅 this SO question on the subject 。请注意,虽然从技术上来说,HTTP 规范可能允许请求正文,但根据您的错误消息,您的客户端库不允许这样做。
要解决此问题,请尝试为 hc
传递 null
值,或仅包含空 JSON 字符串的值(我的意思是使用 ""
,而不是 "{}"
)。
关于java - Google Http 删除正文 (Json),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48342363/