Guest User

Untitled

a guest
Dec 25th, 2017
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. {
  2. 'userName': 'test@gmail.com',
  3. 'password': 'Password!',
  4. 'grant_type': 'password'
  5. }
  6.  
  7. var obj = {
  8. method: 'POST',
  9. headers: {
  10. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  11. },
  12. };
  13. fetch('https://example.com/login', obj)
  14. .then(function(res) {
  15. // Do stuff with result
  16. });
  17.  
  18. var params = {
  19. userName: 'test@gmail.com',
  20. password: 'Password!',
  21. grant_type: 'password'
  22. };
  23.  
  24. var formData = new FormData();
  25.  
  26. for (var k in params) {
  27. formData.append(k, params[k]);
  28. }
  29.  
  30. var request = {
  31. method: 'POST',
  32. headers: headers,
  33. body: formData
  34. };
  35.  
  36. fetch(url, request);
  37.  
  38. var details = {
  39. 'userName': 'test@gmail.com',
  40. 'password': 'Password!',
  41. 'grant_type': 'password'
  42. };
  43.  
  44. var formBody = [];
  45. for (var property in details) {
  46. var encodedKey = encodeURIComponent(property);
  47. var encodedValue = encodeURIComponent(details[property]);
  48. formBody.push(encodedKey + "=" + encodedValue);
  49. }
  50. formBody = formBody.join("&");
  51.  
  52. fetch('http://identity.azurewebsites.net' + '/token', {
  53. method: 'POST',
  54. headers: {
  55. 'Accept': 'application/json',
  56. 'Content-Type': 'application/x-www-form-urlencoded'
  57. },
  58. body: formBody
  59. })
  60.  
  61. var data = new URLSearchParams();
  62. data.append('userName', 'test@gmail.com');
  63. data.append('password', 'Password');
  64. data.append('grant_type', 'password');
  65.  
  66. Object.prototype.serialize = function () {
  67. if (!this) return;
  68.  
  69. var s = []
  70.  
  71. for (var key in this) {
  72. if (this.hasOwnProperty(key)) {
  73. s.push(encodeURIComponent(key) + '=' + encodeURIComponent(this[key]))
  74. }
  75. }
  76.  
  77. return s.join('&')
  78. }
  79.  
  80. obj.serialize()
  81.  
  82. var data = {
  83. a: 1,
  84. b: 2,
  85. c: true
  86. }
  87.  
  88. fetch('/api_url', {
  89. method: 'POST',
  90. headers: {
  91. 'Accept': 'application/json',
  92. 'Content-Type': 'application/x-www-form-urlencoded'
  93. },
  94. body: data.serialize() // see the usage here
  95. })
Add Comment
Please, Sign In to add comment