CloudSole Salesforce REST Feed

I was looking at building a easy way to POST information from your salesforce org to my Public Feed. This feed can be used to publish and data from you salesforce org to the public.

Below is some code sameples on ways to POST to the Public Feed:

curl:

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name": "I love Cloudsole Salesforce Feed", "message": "This is awesome I can post my message on the public feed"}' http://cloudsole-feed.herokuapp.com/sfdc/public/feed

Apex code:

Note:

Make sure to add http://cloudsole-feed.herokuapp.com to you Remote Site Details.

Code:

public with sharing class HttpCalloutToFeed 
{
  public static void sentMessageToFeed()
  { 
    Http http = new Http();
    HttpRequest httpreq = new HttpRequest(); 
    httpreq.setEndpoint('http://cloudsole-feed.herokuapp.com/sfdc/public/feed'); 
    String message = '{"name": "Message from ' + UserInfo.getFirstName() + '", "message": "My Organization ' + UserInfo.getOrganizationName() + ' located in ' + UserInfo.getLocale() + ' loves CloudSole public feed"}';
    httpreq.setHeader('Accept', 'application/json');
    httpreq.setHeader('Content-type', 'application/json');
    httpreq.setBody(message);
    httpreq.setMethod('POST');
    HttpResponse response = http.send(httpreq); 
    System.debug(response.getBody());
  } 
}

Java code:

package com.thysmichels.main;

import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class CloudSoleFeedMain 
{
    public static void main(String[] args) 
    {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://cloudsole-feed.herokuapp.com/sfdc/public/feed");

        try 
        {
            post.setEntity(new StringEntity("{\"name\": \"Message from Java Class\", \"message\": \"This is awesome I can post my message on the public feed\"}"));
            post.setHeader("Accept", "application/json");
            post.setHeader("Content-Type", "application/json");
            HttpResponse response = client.execute(post);
        } catch (IOException e) {
        e.printStackTrace();
        }
    }
}

https://github.com/thysmichels/cloudsole_hibernate_rest_salesforce_feed

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s