0

I am using restheart to provide a restful interface to mongodb. Get method is working good, I'm getting data from database in respons. But in this instance I'm trying to implementing POST request to write data in base. I'm running following code but I'm getting response with code 415 unsupported media type. My test base db1 have one collection testcoll where I'm trying to write a document with fields "name" and "rating"

 public class PostMethodJava {
 public static void main(String[] args) throws IOException {
    URL url;
    try {
        url = new URL("http://127.0.0.1:8080/db1/testcoll/");
        //url = new URL("http://google.com/");
    } catch (Exception et) {
        System.out.println("Data URL is broken");
        return;
    }

    HttpURLConnection hc = null;

    try {
        hc = (HttpURLConnection) url.openConnection();

        String login = "admin:12345678";
        final byte[] authBytes = login.getBytes(StandardCharsets.UTF_8);

        final String encoded = Base64.getEncoder().encodeToString(authBytes);
        hc.addRequestProperty("Authorization", "Basic " + encoded);

        System.out.println("Authorization: " + hc.getRequestProperty("Authorization"));

        //hc.setDoInput(true);
        hc.setDoOutput(true); //<== removed, otherwise 415 unsupported media type
        hc.setUseCaches(false);

        hc.setRequestMethod("POST");
        //hc.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
        hc.setRequestProperty("Accept", "application/json");
    } catch (Exception et) {
        System.out.println("Can't prepare http URL con");
    }

    System.out.println(hc.toString());

    String parameter = "mame=test1&rating=temp";
    int plength = parameter.length();
    byte[] pdata = parameter.getBytes(StandardCharsets.UTF_8);
    try (DataOutputStream out = new DataOutputStream(hc.getOutputStream())){
        out.write(pdata);
    }

    int rc = hc.getResponseCode();

    System.out.println("response code: " + rc);
    System.out.println("response message: " + hc.getResponseMessage());

    }
}

What is wrong and how can I fix it?

konsul777
  • 11
  • 5
  • The `setDoOutput` flag set to true if you intend to use the URL connection for output. The default is false. – fabfas Mar 08 '18 at 09:58
  • I set this flag as true, if I set as false I'm getting exception: Exception in thread "main" java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true) – konsul777 Mar 08 '18 at 10:07
  • Because you are writing to a URLConnection , you have to set it to true. – fabfas Mar 08 '18 at 10:14
  • @fabfas thanks, I set it to true how I written above. But the code isn't working right, I have another issue with code yet. – konsul777 Mar 08 '18 at 10:24

1 Answers1

1

Adding a line:

hc.setRequestProperty("Content-Type","application/json");

and writing the string:

String parameter = "{\"name\":\"doubleabc\",\"rating\":\"allright\"}";

fixed my problem.

konsul777
  • 11
  • 5
  • Exactly. The "Accept" header tells the server which kind of response the client can accept, if the client send a POST request then the "Content-Type" tells the server what type of data is contained in the request. – mturatti Mar 09 '18 at 08:46