Advertisement
Guest User

Untitled

a guest
Jul 26th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. function request(url = "/", method = "POST", body) {
  2. let input = {
  3. headers: {
  4. 'Accept': 'application/json',
  5. 'Content-Type': 'application/json'
  6. },
  7. method
  8. };
  9. if (body) {
  10. input.body = JSON.stringify(body);
  11. }
  12. fetch(`http://localhost:8080${url}`, input)
  13. .then((res) => res.json())
  14. .then((res) => console.log(res))
  15. .catch((err) => console.log(err))
  16. }
  17.  
  18.  
  19. function request(url = "/", method = "POST", body) {
  20. let input = {
  21. headers: {
  22. 'Accept': 'application/json',
  23. 'Content-Type': 'application/json'
  24. },
  25. method
  26. };
  27. if (body) {
  28. input.body = JSON.stringify(body);
  29. }
  30. fetch(`http://localhost:8080${url}`, input)
  31. .then(res => {
  32. const contentType = res.headers.get("content-type");
  33. if (contentType && contentType.indexOf("application/json") !== -1) {
  34. return res.json().then(data => {
  35. console.log(data);
  36. });
  37. }
  38. })
  39. .catch((err) => console.log(err))
  40. }
  41.  
  42.  
  43.  
  44. request("/users/login", "POST", {username: "Danya", password: 123})
  45.  
  46. request("/sample/2", "PUT", {columnA: "Haven", columnB:"Dorre", columnC: "Green", columnD: "Somali", columnE: "L", id:2})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement