+ Start a Discussion
SDeoSDeo 

Calling external rest service giving unsupported media type error

Hi,

 

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.setEndpoint('https://api.infoconnect.com/v1/companies/615869369?resourcetype=basic&apikey=f4702d2cfc7efcbda07160f3e89b4ea1');      

              req.setMethod('GET');         

              req.setTimeout(30000);             

              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}" />:
     </apex:form>
  </apex:page>

The api url works fine when calling directly from browser.

 

Please help me out...

 

Thanks             

kurtz_wolfgangkurtz_wolfgang

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.

 

Thanks,

K

Divyesh DudhatDivyesh Dudhat
Add Content-Type in your request

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

Thanks.