4 Replies Latest reply: Jun 2, 2014 7:50 AM by Bruno L RSS

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

    Bruno L

      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

        • 1. Re: How to add a JSON object to the body of an HTTP POST request
          Sergey.N-Oracle

          Hi Bruno,

           

          please take a look at HttpRequestBuilder.setBody

           

          /Sergey

          • 2. Re: How to add a JSON object to the body of an HTTP POST request
            Bruno L

            Hi Sergey,

            I did but I am still confused : I need a StreamedMessageBody interface. How do I get it?
            Bruno

            • 3. Re: How to add a JSON object to the body of an HTTP POST request
              Sergey.N-Oracle

              You have to create class for this interface

              • 4. Re: How to add a JSON object to the body of an HTTP POST request
                Bruno L

                Thanks  a lot Sergey, once again !

                 

                What I did (for other people that might have the same need) :

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

                Create the class implementing the StreamedMessageBody interface

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

                public class MessageBody implements StreamedMessageBody  {
                   
                    @Override
                    public void write(HttpMessageHeaders headers, java.io.OutputStream bodyStream) {
                   
                      System.out.println("in write method");
                       
                      JsonObject jsonObject = Json.createObjectBuilder()
                     .add("firstname", "John")
                     .add("lastName", "Smith")      
                     .build();
                             
                      JsonWriter jsonWriter=Json.createWriter(bodyStream);
                      jsonWriter.writeObject(jsonObject);
                      jsonWriter.close();

                }

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

                Then, in my main class :

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

                       private void sendJsonHttpRequest(){
                       // default client builder
                        HttpClientBuilder clientBuilder = HttpClientBuilder.getInstance();

                        HttpClient client = clientBuilder.build();

                        HttpRequestBuilder requestBuilder = client.build("http://myURI");
                        requestBuilder.setHeader(HttpHeader.ACCEPT, "text/plain");
                        requestBuilder.setHeader(HttpHeader.CONTENT_TYPE, "application/json");

                        requestBuilder.setMethod(HttpMethod.PUT);
                       
                //Get the StreamedMessageBody interface
                        StreamedMessageBody messageBody= new MessageBody();

                //Update the requestBuilder
                        requestBuilder.setBody(messageBody);

                        // prepared client request is ready for execution
                        HttpRequest request = requestBuilder.build();

                        System.out.println("URI=" + request.getURI());

                        HttpMessageHeaders httpMessageHeaders = request.getHeaders();

                //Invoke the request
                        HttpResponse response = request.invoke();

                    }