如何创建具有 'Content-Type'、授权令牌和 body 的截击 header?
How to create volley header having 'Content-Type', Authorization Token and body?
我正在尝试使用带有身份验证令牌的 oauth 2.0 自定义服务器创建登录 volley post 请求。
Body:
{
"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 body..
传递,请这样做
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;
}
};
我正在尝试使用带有身份验证令牌的 oauth 2.0 自定义服务器创建登录 volley post 请求。 Body:
{
"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 body..
传递,请这样做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;
}
};