Forum Stats

  • 3,838,691 Users
  • 2,262,394 Discussions
  • 7,900,734 Comments

Discussions

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

Bruno L
Bruno L Member Posts: 8
edited Jun 2, 2014 8:50AM in Java ME Embedded

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

Bruno L

Best Answer

Answers

  • Hi Bruno,

    please take a look at HttpRequestBuilder.setBody

    /Sergey

  • Bruno L
    Bruno L Member Posts: 8

    Hi Sergey,

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

  • Sergey.N-Oracle
    Sergey.N-Oracle Member Posts: 99
    Answer ✓

    You have to create class for this interface

    Bruno L
  • Bruno L
    Bruno L Member Posts: 8

    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();

        }

This discussion has been closed.