+ Start a Discussion

Posting to Chatter Rest API

I am trying to post to the Chatter Rest API using the request body instead of a request parameter. This is what my code looks like:


HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'OAuth ' + userInfo.getSessionId());
req.setHeader('Content-Type:', 'application/json');
req.setBody('{ "body" : { "messageSegments" : [{ "type": "Text","text" : "foo bar"}]}}');

Now according to this documentation, if I define the content type as application/x-www-form-urlencoded, it will be looking at the request parameters for the "text" field. However, since I'm defining the content type as application/json, it will be looking at the request body for all the info.


So why is it ignoring the body and telling me that I need to use the "text" field in the request parameters?

Best Answer chosen by Admin (Salesforce Developers) 

Hi Scott,


I think the problem is your call to setHeader('Content-Type:' ...). You are setting the 'Content-Type:' header value instead of 'Content-Type', so the generated HTTP request will have 2 colons here ("Content-Type:: application/json"). This may be confusing the server into thinking you're using an unrecognized content type.



- Chris