I used the following code to post some data to my site. I tested the site with Postman using the same data and it works properly, but when running the following code on Android Studio, response.getStatusLine() returns HTTP/1.1 415 Unsupported Media Type.

       HttpClient httpClient = new DefaultHttpClient();
       HttpPost httpPost = new HttpPost("http://www.example.com/entity/node");
       HttpResponse response = null;
       String responseBody = "";

           JSONObject json = new JSONObject();
           JSONObject c0 = new JSONObject();
           JSONObject c00 = new JSONObject();
           JSONObject c1 = new JSONObject();
           JSONObject c2 = new JSONObject();


           c1.put("value", "test string");
           c2.put("target_id", "article");

           String base64EncodedCredentials = "Basic " + Base64.encodeToString(
                   ("user"+ ":" + "pass").getBytes(),

           StringEntity se = new StringEntity(json.toString());

           se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

           httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");
           httpPost.setHeader("Authorization", base64EncodedCredentials);


           // Execute HTTP Post Request
           response = httpClient.execute(httpPost);

           String jsonResponse = EntityUtils.toString(response.getEntity());
           JSONObject jsonObj = new JSONObject(jsonResponse);

           if (response.getStatusLine().getStatusCode() == 200) {
               Log.v("response ok", "ok response :/");
           } else {
               Log.v("response not ok", "Something went wrong :/");
       catch (Exception e){
           Log.v("Error posting:",e.getMessage());

I tested each of these lines separately and together, but none of them work.

    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));

           httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");

Why does it return HTTP/1.1 415 Unsupported Media Type?

  • Do you perhaps need to set a X-CSRF-Token? – undersound Feb 5 '17 at 19:59
  • Maybe you forgot to add the type of request you are sending as a query parameter? Like so http://www.example.com/entity/node?_format=hal_json – Canutza Jul 19 '17 at 7:45

I have faced same problem with same situation as you described above. After spending 2-3 hours I have got data through:

$formdata = file_get_contents('php://input');

It's worked for me.


Mde, Instead of using HttpClient try using OkHttpClient like this Drupal 8 Android app at https://github.com/onedrupal/One-Drupal-Android.

request = new Request.Builder()
                        .header("Content-Type", "application/json")
OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .addInterceptor(new AddCookiesInterceptor())

AddCookiesInterceptor at https://github.com/onedrupal/One-Drupal-Android/blob/master/app/src/main/java/com/technikh/onedrupal/network/AddCookiesInterceptor.java

public class AddCookiesInterceptor implements Interceptor {
    public Response intercept(Interceptor.Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
Preferences.getDefaultPreferences().getStringSet(Preferences.PREF_COOKIES, new HashSet<>());
        for (String cookie : preferences) {
            builder.addHeader("Cookie", cookie);
        builder.addHeader("X-CSRF-Token", mAuthPreferences.getAuthToken());

        return chain.proceed(builder.build());

Not the answer you're looking for? Browse other questions tagged or ask your own question.