Advertisement
Guest User

Untitled

a guest
Sep 5th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. public class GetRestfulExample {
  2. private final String serviceEndpoint= 'https://devxxxx.service-now.com/api/now/table/incident.do';
  3. public String Response { get; set;}
  4. public String Headers { get; set; }
  5.  
  6. public void fetchData() {
  7. getAndParse('GET');
  8. }
  9.  
  10. public void getAndParse(String GET) {
  11.  
  12. // Get the XML document from the external server
  13. Http http = new Http();
  14. HttpRequest request = new HttpRequest();
  15. String userName = 'xxx';
  16. String password = 'xxxx';
  17.  
  18. // Specify the required user name and password to access the endpoint
  19. // As well as the header and header information
  20. Blob headerValue = Blob.valueOf(userName + ':' + password);
  21. String authorizationHeader = 'BASIC ' +
  22. EncodingUtil.base64Encode(headerValue);
  23. request.setEndpoint(serviceEndpoint);
  24.  
  25. request.setMethod('GET');
  26. request.setHeader('Content-Type', 'application/json');
  27. // Header info with remote server user name and password
  28. request.setHeader('Authorization', authorizationHeader);
  29. // timeout in milliseconds
  30. request.setTimeout(120000);
  31.  
  32.  
  33.  
  34. HttpResponse res = http.send(request);
  35.  
  36. System.debug(res.getBody());
  37. this.response=res.getBody();
  38. }
  39. }
  40.  
  41. <apex:page controller="GetRestfulExample" action="{!fetchData}" contentType="text/plain">
  42. {!response}
  43.  
  44. setHeader('Accept','application/json');
  45.  
  46. HttpRequest req = new HttpRequest();
  47. Http http = new Http();
  48.  
  49. Blob headerValue = Blob.valueOf(credentials.get('Username') + ':' + credentials.get('Password'));
  50. String authorizationHeader = 'BASIC ' +
  51. EncodingUtil.base64Encode(headerValue);
  52. req.setHeader('Authorization', authorizationHeader);
  53. req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
  54.  
  55.  
  56. req.setMethod('POST');
  57. req.setBody(....);
  58.  
  59. String url = ....;
  60.  
  61. req.setEndPoint(url);
  62.  
  63. HTTPResponse resp = http.send(req);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement