我正在尝试使用自定义服务器和 oauth 2.0 使用身份验证 token 创建登录 Volley 发布请求。 正文:
{
"customer": {
"email": "example@gmail.com",
"password":"12345"
}
}
我试过了
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer .....UzI1NiIsInR5cCI6IkpXVCJ9.....");
headers.put("email",username.toString());
headers.put("password",password.toString());
headers.put("customer",headers.toString());
Log.e("Json created", headers.toString());
return headers;
}
最佳答案
如果您想将数据作为 JSON 正文传递,请这样做..
String body = "{\"customer\":{\"email\":"+ username + ",\"password\":"+ password +"}}";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, response -> {
//response here
}, error -> {
//exceptions here
}) {
@Override
public byte[] getBody() throws AuthFailureError {
return body.getBytes();
}
@Override
public HashMap<String, String> getHeaders() throws AuthFailureError {
final HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer .....UzI1NiIsInR5cCI6IkpXVCJ9.....");
return headers;
}
};
关于java - 如何创建具有 'Content-Type' 、授权 token 和正文的 Volley header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59653608/