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

Calling external rest service giving unsupported media type error



I am trying to call infoconnect API through apex class and page. But its giving me error "Unsupported media type".


Following is my code :


Apex class code:


public class CalloutSample {   

   public String getData()  {

              Http http = new Http();      

              HttpRequest req = new HttpRequest();      




              req.setHeader('Content-Type', 'application/json');                    

              HttpResponse res = http.send(req);                 

              return res.getStatus();                 




Apex page code :


<apex:page controller="CalloutSample">
     <apex:form >
         <apex:outputLabel value="{!Data}" />:

The api url works fine when calling directly from browser.


Please help me out...




The reason why you are getting the error is because Infoconnect server is refusing content of the request (because you aren't setting any body of the request).


To accept the response object content in form of JSON content, you need to set it  : req.setHeader('Accept', 'application/json');

instead of req.setHeader('Content-Type', 'application/json');       



Hope this helps.


Please mark the question as solved if this response resolves your problem.




Divyesh DudhatDivyesh Dudhat
Add Content-Type in your request

 req.setHeader('Content-Type', 'application/json');