0

I have been trying to call an external service from apex. My Attempts so far to view the incidents ..

public class GetRestfulExample {
 private final String serviceEndpoint= 'https://devxxxx.service-now.com/api/now/table/incident.do';
public String Response { get; set;}
public String Headers { get; set; }

public void fetchData() {
    getAndParse('GET');
 }

public void getAndParse(String GET) {

// Get the XML document from the external server
Http http = new Http();
HttpRequest request = new HttpRequest();
String userName = 'xxx';
  String password = 'xxxx';

  // Specify the required user name and password to access the endpoint 
  // As well as the header and header information 
  Blob headerValue = Blob.valueOf(userName + ':' + password);
  String authorizationHeader = 'BASIC ' +
  EncodingUtil.base64Encode(headerValue);
request.setEndpoint(serviceEndpoint);

request.setMethod('GET');
  request.setHeader('Content-Type', 'application/json');
  // Header info with remote server user name and password
  request.setHeader('Authorization', authorizationHeader);
  // timeout in milliseconds       
  request.setTimeout(120000); 



  HttpResponse res = http.send(request);

System.debug(res.getBody());
 this.response=res.getBody();
 }
}

and vf...

 <apex:page controller="GetRestfulExample" action="{!fetchData}" contentType="text/plain">
 {!response}

I get this error and can't find where the headers are located...

{"error":{"message":"Unsupported value for header: Accept=text/html, image/gif, image/jpeg, *; q=.2, /; q=.2","detail":"Unsupported value for header: Accept=text/html, image/gif, image/jpeg, *; q=.2, /; q=.2 Check logs for error trace or enable glide.rest.debug property to verify REST request processing"},"status":"failure"}

I'm a beginner for using rest api, I would be grateful if I'm pointed to the right direction.

Thanks in advance!

1 Answer 1

1

Per the documentation of the Service Now API you MUST set the 'Accept' header for a GET request so add

http://wiki.servicenow.com/index.php?title=REST_API#Headers

Each of the supported methods can be overridden if you set the X-http-method-override header. The Accept and Content-Type request headers are required for proper data formatting. These request headers have the following valid values:

Accept: application/json, application/xml Content-Type: application/json, application/xml POST, PUT, and PATCH operations require you to provide both headers. The GET and DELETE operations require only the Accept header. Failing to provide the required headers results in a 400 Bad Request error.

try adding

setHeader('Accept','application/json');

Also, here is a known working API from Apex, although the endpoint API requirements may be a bit different.

HttpRequest req = new HttpRequest();
Http http = new Http();

Blob headerValue = Blob.valueOf(credentials.get('Username') + ':' + credentials.get('Password'));
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');


req.setMethod('POST');
req.setBody(....);

String url = ....;

req.setEndPoint(url);

HTTPResponse resp = http.send(req);
2
  • I have included "Accept", but there's no change
    – MnZ
    Oct 20, 2015 at 16:03
  • @MnZ - You previously said this did not help, is there anything I should add to the answer to make it more useful since you marked it as accepted?
    – Eric
    Nov 13, 2015 at 14:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .