gpt4 book ai didi

java - Volley DELETE 请求抛出 400 BAD REQUEST

转载 作者:行者123 更新时间:2023-12-01 18:27:38 25 4
gpt4 key购买 nike

我尝试向我的服务器发送 DELETE 请求,但收到 400 bad request 错误代码。我搜索了很多,但找不到任何有帮助的解决方案。当我用 Postman 尝试时,请求工作正常。

这是 Postman 中的 curl 命令的样子:

curl --location --request DELETE 'https://blablabla.de' \
--header 'Content-Type: application/json' \
--data-raw '{
"deviceId":"33",
"factoryReset":"0"
}'

这是我的 Java 代码:

    public void unlinkDevice(String deviceId) {
Log.d(LOG_TAG, "unlinkDevice: " + deviceId);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("deviceId", deviceId);
jsonObject.put("factoryReset", 0);
} catch (JSONException e) {
e.printStackTrace();
}

JsonObjectRequest request = new JsonObjectRequest(Request.Method.DELETE, url, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
listener.onDeviceUnlink(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
listener.onError(error);
}
}) {

@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Cookie", StartApplication.getCookie());
return headers;
}
};

queue.addToRequestQueue(request);
}

我也尝试在 header 中不设置 Content-Type ,但是当我收到 415 错误时,我还删除了 getBodyContentType() 方法,但这也没有改变任何内容。

还有其他可以提供帮助的想法吗?

最佳答案

好吧,我找到了原因...当您使用 DELETE 请求时,正文会被忽略

关于java - Volley DELETE 请求抛出 400 BAD REQUEST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60208362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com