function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kodeXkodeX 

Adding comments needs HttpEntityBody

Rest format for commenting is-

../chatter/feed-items/0D590000006SY64CAG/comments?text=my comment added successfully!

 

 

Above, Underlined portion is feeditemid, bold text is my comment.

 

But when i execute,

 

It gives error as -

[{"message":"The HTTP entity body is required, but this request has no entity bo
dy.","errorCode":"JSON_PARSER_ERROR"}]

 

There are methods-> setRequestHeader, setParams in HttpMethod interface,

but there is no method like "setEntityBody".

 

How to set the entity body in HttpMethod?

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
ChrisOctagonChrisOctagon

Every HTTP client library should let you set the body. You might find it helpful to read some tutorials on how HTTP requests are structured.

 

I believe in Java the method is PostMethod.SetRequestEntity(): http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/methods/EntityEnclosingMethod.html#setRequestEntity(org.apache.commons.httpclient.methods.RequestEntity)

All Answers

ChrisOctagonChrisOctagon

What "content-type" header value are you providing? If you are specifying "application/json" then you can't use query parameters like "text".

ChrisOctagonChrisOctagon

If you are using a content-type of "application/json" then "HTTP Entity Body" refers to the JSON that is expected to be found in the request body.

 

Remember:

"content-type" indicates the type of input you are providing

"accept-type" indicates the type of output you are expecting back

kodeXkodeX

Hey Chris,

Same question- How to specify json/xml Http entity body in java?

 

Suppose for new comment entity body is-

{ "body" :
         {
          "messageSegments" : [
                              {
                              "type": "Text",
                              "text" : "My comment added successfully"
                              }
                              ]
         }
}

How do i actually put the above body in HttpMethod java code?

Considering, messageSegments array of <key,value> pairs, Could you provide the code snippet?

kodeXkodeX

I tried doing the following with xml body-

pm - instance of Http PostMethod
client- instance of HttpClient
pm.setRequestHeader("Content-Type", "application/xml"); pm.setRequestHeader("Accept-type", "text/xml"); String body="<messageSegments>" + "<type>Text</type><text>My Comment</text>" + "</messageSegments></Body>"; pm.setRequestHeader("<Body>", body); client.execute(pm);

Still it is returning the same entity body error!

How to put entity body in code?

ChrisOctagonChrisOctagon

Don't call "setRequestHeader" to set the body. What you are doing here is creating an HTTP header value called "<Body>". This won't work.

 

The "header" and the "request body" are 2 completely separate parts of your HTTP request. Your programming language should give you a method to let you either explicitly set the request body or to write it in with a stream.

kodeXkodeX

Hey Chris,

I did not find any java method to set request body in HttpMethod.

Is there any other workaround?

ChrisOctagonChrisOctagon

Every HTTP client library should let you set the body. You might find it helpful to read some tutorials on how HTTP requests are structured.

 

I believe in Java the method is PostMethod.SetRequestEntity(): http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/methods/EntityEnclosingMethod.html#setRequestEntity(org.apache.commons.httpclient.methods.RequestEntity)

This was selected as the best answer
kodeXkodeX

Thanks CHRIS!!

 

It WORKED!!

import org.apache.commons.httpclient.methods.StringRequestEntity;

 

Here is the code-

String req="{ \"body\" :{\"messageSegments\" : [{\"type\": \"Text\",\"text\" :" +" \"First post on the group!\"}]}}";
 
pm.setRequestEntity(new StringRequestEntity(req));
client.executeMethod(pm);

Import StringRequestEntity