Advertisement
Guest User

Untitled

a guest
Apr 7th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. String content = '{"Name": "Some acoount", "Phone":"9742882954"}';
  2. HttpRequest req = new HttpRequest();
  3. req.setEndpoint('https://ap1.salesforce.com/services/apexrest/Accountacc');
  4. req.setMethod('POST');
  5.  
  6. String username = 'sarvesh01@gmail.com';
  7. String password = 'parvati@3';
  8.  
  9. Blob headerValue = Blob.valueOf(username + ':' + password);
  10. String authorizationHeader = 'BASIC ' +
  11. EncodingUtil.base64Encode(headerValue);
  12. req.SetBody(content);
  13. req.setHeader('Content-Type', 'application/json; charset=utf-8');
  14. req.setHeader('Authorization', authorizationHeader);
  15. req.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
  16. Http http = new Http();
  17. HTTPResponse res = http.send(req);
  18. System.debug(res.getBody());
  19.  
  20. @RestResource(urlMapping='/Accountacc/*')
  21. global with sharing class callAccount {
  22.  
  23. @HttpPost
  24. global static String doPost() {
  25.  
  26. RestRequest request = RestContext.request;
  27. RestResponse response = RestContext.response;
  28. String jSONRequestBody=request.requestBody.toString().trim();
  29. Account accObj = (Account)JSON.deserializeStrict(jSONRequestBody,Account.class);
  30. insert accObj;
  31. return accObj.Id;
  32. }
  33. @HttpGet
  34. global static Account doGet() {
  35.  
  36. RestRequest request = RestContext.request;
  37. RestResponse response = RestContext.response;
  38. String accountId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
  39. Account acc= [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accountId];
  40. return acc;
  41. }
  42. }
  43.  
  44. HttpRequest req = new HttpRequest();
  45. req.setHeader('Authorization', 'OAuth ' + accessToken);
  46. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement