Kiryk
Kiryk

Reputation: 3

POST header with token bearer

I am trying to POST authorization token in header using OkHttp. I am trying something like this, but unfortunately, i've got 415 error in debug mode. 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();
            }
        }
    });

Upvotes: 0

Views: 397

Answers (1)

Vasia Zaretskyi
Vasia Zaretskyi

Reputation: 347

415 code stands for unsupported media type. Here is what you can do in this case:

  1. If you have such a possibility, consider using application/json or something like that. Usually APIs have such option.

  2. text/x-markdown is not used widely, try to replace it with text/markdown.

  3. If nothing works for you, try getting text/plain and create a small parcer that will process markdown

Upvotes: 1

Related Questions