【发布时间】:2020-05-29 04:37:34
【问题描述】:
我尝试向我的服务器发送 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);
}
我也尝试在没有在标题中设置 Content-Type 的情况下,但是当我收到 415 错误并且我还删除了 getBodyContentType() 方法时,但这也没有改变。
还有其他可以提供帮助的想法吗?
【问题讨论】:
-
你能确认 factoryReset 是 String 上的整数吗?如果它是一个字符串,并且您正在传递可能导致问题的整数。
-
在服务器上将其句柄作为字符串,但当我将“factoryReset”,0 更改为“factoryReset”,“0”时,我得到了 400 返回
-
您的邮递员运行良好吗?通过将 factoryReset 更改为整数
-
是的,这也通过 Postman 工作
-
您能否确认您尝试访问的端点对于应用程序和邮递员而言是相同的。
标签: java android json request android-volley