How to add json to the body of an http post in java

35,841

You should set the Content-Type header to "application/json". Everything else looks good.

Share:
35,841
Toran Billups
Author by

Toran Billups

Swiss Army Knife: Elixir, React & Tailwind

Updated on May 14, 2020

Comments

  • Toran Billups
    Toran Billups almost 4 years

    I'm trying to post some JSON data in java for an Android app I'm working on. Is the below valid or do I need to push the JSON string in a different way?

    HttpPost httpost = new HttpPost("http://test.localhost");
    httpost.setEntity(new StringEntity("{\"filters\":true}"));
    httpost.setHeader("Accept", "application/json");
    httpost.setHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
    //... other java code to execute the apache httpclient
    

    Thank you in advance