[英]POST header with token bearer
I am trying to POST
authorization token in header using OkHttp
.我正在尝试使用
POST
在 header 中OkHttp
授权令牌。 I am trying something like this, but unfortunately, i've got 415 error
in debug mode.我正在尝试这样的事情,但不幸的是,我在调试模式下遇到了
415 error
。 Thanks in advance.提前致谢。
private void Logout() throws IOException{
String postBody = "test post";
OkHttpClient okHttpClient= new OkHttpClient();
RequestBody body = RequestBody.create(
MediaType.parse("text/x-markdown"), postBody);
Request request = new Request.Builder()
.url("ABC")
.addHeader("Authorization",tokenBearer)
.post(body)
.build();
okHttpClient.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(okhttp3.Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
if(response.isSuccessful()){
String message= response.body().toString();
Toast.makeText(getActivity(),message,Toast.LENGTH_LONG).show();
}
}
});
415 code stands for unsupported media type . 415代码代表不受支持的媒体类型。 Here is what you can do in this case:
在这种情况下,您可以执行以下操作:
If you have such a possibility, consider using application/json
or something like that.如果您有这种可能性,请考虑使用
application/json
或类似的东西。 Usually APIs have such option.通常 API 有这样的选项。
text/x-markdown
is not used widely, try to replace it with text/markdown
. text/x-markdown
没有被广泛使用,请尝试将其替换为text/markdown
。
If nothing works for you, try getting text/plain
and create a small parcer that will process markdown如果您没有任何效果,请尝试获取
text/plain
并创建一个小型 parcer 来处理 markdown
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.