Bad Content-Type on SugarCRM REST API Interface

Today I discovered a really tedious bug on SugarCRM, one of those bugs that take away precious time and you do not expect. The object of the bug are the REST services of SugarCRM. The version on which I have found the problem is the 6.1 Community Edition, but I think also the other editions (Professional and Enterprise) are affected. The versions of the REST Service are: 2 and 3, for more information see document at http://www.sugarcrm.com/crm/support/documentation/SugarCommunityEdition/6.1/-docs-Developer_Guides-Sugar_Developer_Guide_6.1.0-Chapter%202%20Application%20Framework.html#9000259.

Calling a REST service, requiring the response in JSON format, the server responds with an incorrect Content-Type: text/html instead of application/json.

The incorrect value of the Content-Type can cause problems with some frameworks used to consume RESTful JSON services, one of many frameworks is Jersey. See the error returned from Jersey to resulting from the wrong Content-Type.

11-apr-2011 23.07.07 com.sun.jersey.api.client.ClientResponse getEntity
GRAVE: A message body reader for Java class
it.lab.shirus.sugarcrm.client.jaxb.EntryValue,
and Java type class it.lab.shirus.sugarcrm.client.jaxb.EntryValue,
and MIME media type text/html was not found
11-apr-2011 23.07.07 com.sun.jersey.api.client.ClientResponse getEntity
GRAVE: The registered message body readers compatible with the MIME media type are:
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy

For more information see the following documents available on the network:

Following is an example of calling the login service via Firefox Browser and Firebug as debugger.

Bad Content-Type SugarCRM REST API Interface

Bad Content-Type SugarCRM REST API Interface

View JSON Response

View JSON Response

The solution is simple: add one line of code to the class SugarRestJSON defined in service/core/REST/SugarRestJSON.php. The line of code sets the correct Content-Type takes to respond to the service (see the method body generateResponse ()).

/**
 * It will json encode the input object and echo's it
 *
 * @param array $input - assoc array of input values:
 * key = param name, value = param type
 * @return String - echos json encoded string of $input
 */
 function generateResponse($input){
 $json = getJSONObj();
 ob_clean();
 header('Content-Type: application/json; charset=UTF-8');
 if (isset($this->faultObject)) {
 $this->generateFaultResponse($this->faultObject);
 } else {
 echo $json->encode($input);
 }
 } // fn

Following the service responses after applying the Fix.

Content-Type after applying the fix.

Content-Type after applying the fix.

View JSON Response after applying the fix (JSON View Firefox Plugin).

View JSON Response after applying the fix (JSON View Firefox Plugin).

See SugarCRM Bug #43368
For any questions please feel free to leave your comments.

Antonio Musarra

I began my journey into the world of computing from an Olivetti M24 PC (http://it.wikipedia.org/wiki/Olivetti_M24) bought by my father for his work. Day after day, quickly taking control until … Now doing business consulting for projects in the enterprise application development using web-oriented technologies such as J2EE, Web Services, ESB, TIBCO, PHP.

You may also like...