Skip to Main Content

Embedded Technologies

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to add a JSON object to the body of an HTTP POST request

Bruno LJun 2 2014 — edited Jun 2 2014

Hello,

I am using Java 8 ME Embedded and I would like to add a Json object in the body of an http request.

I would like to do something like :

-----------------------------------------------------------------------------------------

     HttpClientBuilder clientBuilder = HttpClientBuilder.getInstance();

        HttpClient client = clientBuilder.build();

        HttpRequestBuilder requestBuilder = client.build("my_URI");
        requestBuilder.setHeader(HttpHeader.ACCEPT, "text/plain");

[...]

        requestBuilder.setMethod(HttpMethod.POST);


        HttpRequest request = requestBuilder.build();

//creating the Json object to be sent in the http request body
      JsonObject myJsonObj= Json.createObjectBuilder()
     .add("firstname", "John")
     .add("lastname", "XXX")
     .build();

//This part, I am a bit confused : how to get the OutputStream in which I should put the Json object?

JsonWriter jsonWriter = Json.createWriter(java.io.OutputStream out);

jsonWriter.writeObject(myJsonObj);

jsonWriter.close();

     //invoking the http request

      HttpResponse response = request.invoke();

-----------------------------------------------------------------------------------------

Do you have any tip, please ?
Thanks in advance.

Bruno

This post has been answered by Sergey.N-Oracle on Jun 2 2014
Jump to Answer

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jun 30 2014
Added on Jun 2 2014
4 comments
7,373 views