我试图向我的服务器发送一个DELETE请求,但是我得到了一个400错误的请求错误代码。我搜索了很多,但我找不到任何有帮助的解决方案。当我尝试使用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);
}我也尝试不在头部设置Content-Type,但是当我得到一个415错误时,我还删除了getBodyContentType()方法,但这也没有改变什么。
还有其他可以帮助你的想法吗?
发布于 2020-02-13 21:53:31
好吧我找到原因了..。在volley中,当您使用DELETE请求时,主体被忽略
https://stackoverflow.com/questions/60208362
复制相似问题