如何创建具有'Content-Type',授权令牌和主体的凌空标头?

问题描述 投票:0回答:1

我正在尝试使用带有身份验证令牌的oauth 2.0和自定义服务器来创建登录截击发布请求。正文:

{
"customer": {
    "email": "[email protected]",
    "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;
}
java android json android-volley
1个回答
0
投票

如果要作为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;
    }
};
© www.soinside.com 2019 - 2024. All rights reserved.