Skip to content

Arifurrex/api-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

api-calling

frontend āĻ āφāĻŽāϰāĻž designe āĻ•āϰ⧇ āĻĨāĻžāĻ•āĻŋ āφāϰ backend/āϏāĻžāĻ°ā§āĻ­āĻžāϰ āĻ āφāĻŽāϰāĻž āĻŦāĻŋāĻ­āĻŋāĻ¨ā§āύ āϰāĻ•āĻŽ āϞāϜāĻŋāĻ• āϞāĻŋāϖ⧇ āĻĨāĻžāĻ•āĻŋ āφāϰ āĻāχ āϞāϜāĻŋāĻ• āĻ—āϞ⧋ āĻĻāĻŋā§Ÿā§‡ āφāĻŽāϰāĻž database āϗ⧁āϞ⧋ āĻšā§āϝāĻžāĻ¨ā§āĻĄā§‡āϞ āĻ•āϰ⧇ āĻĨāĻžāĻ•āĻŋ

suppose āφāĻĒāύāĻŋ frontend āĻ āĻ•āĻžāϜ āĻ•āϰāϛ⧇āύ āφāϰ āφāĻĒāύāĻžāϰ data āϞāĻžāĻ—āĻŦ⧇ āĨ¤ āϤāĻžāĻšāϞ⧇ data āϗ⧁āϞ⧋ āφāĻĒāύāĻŋ āĻ•āĻŋāĻ­āĻžāĻŦ⧇ āĻĒāĻžāĻŦ⧇āύ āĨ¤ āφāĻŽāϰāĻž āϜāĻžāύāĻŋ data āϗ⧁āϞ⧋ database āĻ āĻ°ā§Ÿā§‡āϛ⧇ āφāϰ database āϕ⧇ handle āĻ•āϰāϛ⧇ āĻŽā§‚āϞāϤ āϏāĻžāĻ°ā§āĻ­āĻžāϰ āĨ¤ āĻāϰ āĻŽāĻžāύ⧇ frontend āĻĨ⧇āϕ⧇ server āĻ call āĻĻāĻŋāϤ⧇ āĻšāĻŦ⧇ data āĻĒāĻžāĻ“ā§ŸāĻžāϰ āϜāĻ¨ā§āϝ⧇ āĨ¤ āϏ⧇āϟāĻž āĻšāϤ⧇ āĻĒāĻžāϰ⧇ database āĻĨ⧇āϕ⧇ data āĻĒāĻžāĻ“ā§ŸāĻžāϰ āϜāĻ¨ā§āϝ⧇ āĻšāϤ⧇ āĻĒāĻžāϰ⧇ database āĻ data update āĻ•āϰāϤ⧇ āĻ…āĻĨāĻŦāĻž frontend āĻĨ⧇āϕ⧇ āύ⧁āϤāύ data send āĻ•āϰāϤ⧇ āϚāĻžāχāĻ›āĻŋ āĻ•āĻŋāĻ‚āĻŦāĻž data delete āĻ•āϰāϤ⧇ āϚāĻžāχāĻ›āĻŋ

āĻāχ āĻŦāĻŋāĻ­āĻŋāĻ¨ā§āύ āϧāϰāύ⧇āϰ request āφāĻŽāϰāĻž frontend āĻĨ⧇āϕ⧇ server āĻ send āĻ•āϰāϤ⧇ āϚāĻžāχāĻ›āĻŋ āĨ¤ āϏ⧇āχ āϜāĻ¨ā§āϝ⧇ āφāĻŽāĻžāϰ āĻāĻ•āϟāĻž āϞāĻŋāĻ‚āĻ• āϞāĻžāĻ—āĻŦ⧇ āĨ¤ āϏ⧇āχ āϞāĻŋāĻ‚āĻ• āϟāĻž āĻšāĻšā§āϛ⧇ api

server āϝāĻ–āύ āϤ⧈āϰāĻŋ āĻ•āϰāĻž āĻšā§Ÿ āϤāĻ–āύ āĻāχ api āϗ⧁āϞ⧋ āϤ⧈āϰāĻŋ āĻ•āϰāĻž āĻšā§Ÿ āĨ¤ āĻāχ āϜāĻ¨ā§āϝ⧇ āϝāĻžāϤ⧇ frontend āĻĨ⧇āϕ⧇ server āĻāϰ āĻŽāϧ⧇ connection āĻāϰ āϜāĻ¨ā§āϝ⧇ āĻāχ api āϗ⧁āϞ⧋ āϤ⧈āϰāĻŋ āĻ•āϰāĻž āĻšā§Ÿ

āϤāĻžāĻšāϞ⧇ api āĻ—āϞ⧋ āĻšāϞ āĻāĻ•āϟāĻž link āĻŦāĻž url āϝāĻžāϰ āĻŽāĻĻā§āϝāĻŽā§‡ āĻĢā§āϰāĻ¨ā§āϟāĻāĻ¨ā§āĻĄ āĻāϰ āϏāĻžāϤ⧇ āĻŦāĻ•āϕ⧇āĻ¨ā§āĻĄ āĻāϰ relation āĻ•āϰāϤ⧇ āĻĒāϰāĻŋ āĻāĻŦāĻ‚ āĻŦāĻŋāĻ­āĻŋāĻ¨ā§āύ āϧāϰāϪ⧇ request āϏ⧇āĻ¨ā§āĻĄ āĻ•āϰāϤ⧇ āĻĒāĻžāϰāĻŋ āϏ⧇āχ request āĻšāϤ⧇ āĻĒāĻžāϰ⧇ get ,put,fetch ,update ,patch,delete,post

APIs, short for Application Programming Interfaces, āϏāĻĢā§āϟāĻ“āϝāĻŧā§āϝāĻžāϰ āĻĨ⧇āϕ⧇ āϏāĻĢā§āϟāĻ“āϝāĻŧā§āϝāĻžāϰ āχāĻ¨ā§āϟāĻžāϰāĻĢ⧇āϏāĨ¤ Meaning, āϤāĻžāϰāĻž āĻŦāĻŋāĻ­āĻŋāĻ¨ā§āύ āĻ…ā§āϝāĻžāĻĒā§āϞāĻŋāϕ⧇āĻļāύāϕ⧇ āĻāϕ⧇ āĻ…āĻĒāϰ⧇āϰ āϏāĻžāĻĨ⧇ āĻ•āĻĨāĻž āĻŦāϞāĻžāϰ āĻāĻŦāĻ‚ āϤāĻĨā§āϝ āĻŦāĻž āĻ•āĻžāĻ°ā§āϝāĻ•āĻžāϰāĻŋāϤāĻž āĻŦāĻŋāύāĻŋāĻŽāϝāĻŧ āĻ•āϰāĻžāϰ āĻ…āύ⧁āĻŽāϤāĻŋ āĻĻ⧇āϝāĻŧāĨ¤

javascript āĻĨ⧇āϕ⧇ 4 āϟāĻž āωāĻĒāĻžā§Ÿā§‡ api call āĻ•āϰāϤ⧇ āĻĒāϰāĻŋ

  1. XMLHttpRequest
  2. fetch
  3. axios
  4. jquery





  • fetch() has replace XMLHttpRequest
  • fetch() - global method for making HTTP request
  • 2 way to call -than, async await





    • fetch() is easy to use comapre to XMLHttpRequest
    • fetch() returns a promise
    • returned promise can only handle network error
    • does not support all the older browser

XMLHttpRequest

is one old request .

console.log(window)

Screenshot 2022-12-17 004140

const getData = function(){
   const xhr = new XMLHttpRequest();
   //i will get every property and method and event from creating new object of XMLHttpRequest
   console.log(xhr);  
}
getData();

Screenshot 2022-12-17 115030

i will get every property and method and event from creating new object of XMLHttpRequest

  1. event - onload(), onerror()
  2. property- response, responseType ,
  3. function - open() ,send() ,setRequestHeader()

step - 1

const getData = function(){
   const xhr = new XMLHttpRequest(); 
}
getData();

step - 2 open()

const getData = function(){
   const xhr = new XMLHttpRequest();
   xhr.open('GET','URL')  
}
getData();

xmlHttpRequest() object āĻāϰ open() method call āĻ•āϰāĻŦ āĨ¤ open() method 2 āϟāĻž argument receive āĻ•āϰāĻŦ⧇ āĨ¤

step - 3 set fake api in open()

fake api link

const getData = function(){
   const xhr = new XMLHttpRequest();
   xhr.open('GET','https://jsonplaceholder.typicode.com/posts/1')  
}
getData();

step - 4 send()

const getData = function(){
   const xhr = new XMLHttpRequest();
   xhr.open('GET','https://jsonplaceholder.typicode.com/posts/1');
   xhr.send();  
}
getData();

step - 5 (newtwork e data show korbe)

Screenshot 2022-12-18 112131

step - 6 (console e data āĻ data show āĻ•āϰāĻžāϰ āϜāĻ¨ā§āϝ⧇ onload event use āĻ•āϰāϤ⧇ āĻšāĻŦ⧇ )

const getData = function(){
   const xhr = new XMLHttpRequest();
   xhr.open('GET','https://jsonplaceholder.typicode.com/posts');
   xhr.onload = function(){
      let data = xhr.response;
      console.log(data);
   };
   xhr.send();  
}
getData();

onload event āĻāϰ āĻŽāĻĻā§āϝ⧇ data process āĻ•āϰāĻŦ āĨ¤ data āϟāĻž xhr.response property āĻāϰ āĻŽāĻĻā§āϝ⧇ āϤāĻ•āĻŦ⧇ data =xhr.response console.log(data); āĻāϰ āĻĢāϞ⧇ data console āĻ show āĻ•āϰāĻŦ⧇

step-7(json format to javascript object format) JSON.parse()

Screenshot 2022-12-18 115612

const getData = function(){
   const xhr = new XMLHttpRequest();
   xhr.open('GET','https://jsonplaceholder.typicode.com/posts');
   xhr.onload = function(){
      let data = xhr.response;
      console.log(JSON.parse(data));
   };
   xhr.send();  
}
getData();

image

step -10 onerror

const getData = function(){
   const xhr = new XMLHttpRequest();
   xhr.open('GET','https://jsonplaceholder.typicode.como/posts');
   xhr.onload = function(){
      let data = xhr.response;
      console.log(JSON.parse(data));
   };
   xhr.onerror = function(){
      console.log('error is here');
   }
   xhr.send();  
}
getData();

onerror event will find the error i change here https://jsonplaceholder.typicode.com/posts to https://jsonplaceholder.typicode.como/posts

onerror event will find the error

Screenshot 2022-12-18 130106

āĻāĻ­āĻžāĻŦ⧇ request make āĻ•āϰāϤ⧇ āĻšā§Ÿ XMLHttpRequest() āĻāϰ āĻŽāĻĻā§āϝāĻŽā§‡

āĻ•āĻŋāĻ­āĻžāĻŦ⧇ multiple request āĻ•āϰāĻž āĻšā§Ÿ

const makeRequest = function(method,url){
   const xhr = new XMLHttpRequest();
   xhr.open(method,url);
   xhr.onload = function(){
      let data = xhr.response;
      console.log(JSON.parse(data));
   };
   xhr.onerror = function(){
      console.log('error is here');
   }
   xhr.send();  
}


const getData = function(){
   makeRequest('GET','https://jsonplaceholder.typicode.com/posts');
}
getData();

āĻ•āĻŋāĻ­āĻžāĻŦ⧇ post request āĻ•āϰāĻž āĻšā§Ÿ

method āĻšāĻŦ⧇ post url āĻšāĻŦ⧇

  1. title body userid āĻĻāĻŋāϤ⧇ āĻšāĻŦ⧇
  2. open() āĻāĻŦāĻ‚ send() function āĻāϰ āĻŽāĻžāĻœā§‡ etRequestHeader('Content-type','application/json') āĻĻāĻŋāϤ⧇ āĻšāĻŦ⧇
  3. data āϕ⧇ JSON.stringify(data1) āĻ•āϰāϤ⧇ āĻšāĻŦ⧇
  4. send(JSON.stringify(data1))
const makeRequest = function(method,url,data1){
   const xhr = new XMLHttpRequest();
   xhr.open(method,url);
   xhr.setRequestHeader('Content-type','application/json');
   xhr.onload = function(){
      let data = xhr.response;
      console.log(JSON.parse(data));
   };
   xhr.onerror = function(){
      console.log('error is here');
   }
   xhr.send(JSON.stringify(data1));  
}


const getData = function(){
   makeRequest('GET','https://jsonplaceholder.typicode.com/posts');
}
const postData = function(){
   makeRequest('POST','https://jsonplaceholder.typicode.com/posts',{
      title: 'foo',
      body: 'bar',
      userId: 1,
   });
}
postData();

Screenshot 2022-12-18 194550

āĻ•āĻŋāĻ­āĻžāĻŦ⧇ update request āĻ•āϰāĻž āĻšā§Ÿ

const makeRequest = function(method,url,data1){
   const xhr = new XMLHttpRequest();
   xhr.open(method,url);
   xhr.setRequestHeader('Content-type','application/json');
   xhr.onload = function(){
      let data = xhr.response;
      console.log(JSON.parse(data));
   };
   xhr.onerror = function(){
      console.log('error is here');
   }
   xhr.send(JSON.stringify(data1));  
}


const getData = function(){
   makeRequest('GET','https://jsonplaceholder.typicode.com/posts');
}
const postData = function(){
   makeRequest('POST','https://jsonplaceholder.typicode.com/posts',{
      title: 'foo',
      body: 'bar',
      userId: 1,
   });
}
const updateData = function(){
   makeRequest('PUT','https://jsonplaceholder.typicode.com/posts/1',{
      id:1,
      title: 'foota',
      body: 'barto',
      userId: 1,
   });
}

updateData();

Screenshot 2022-12-18 200449

update single date

const makeRequest = function(method,url,data1){
   const xhr = new XMLHttpRequest();
   xhr.open(method,url);
   xhr.setRequestHeader('Content-type','application/json');
   xhr.onload = function(){
      let data = xhr.response;
      console.log(JSON.parse(data));
   };
   xhr.onerror = function(){
      console.log('error is here');
   }
   xhr.send(JSON.stringify(data1));  
}

//get data
const getData = function(){
   makeRequest('GET','https://jsonplaceholder.typicode.com/posts');
}

// single data update
const updateSingleData = function(){
   makeRequest('PATCH','https://jsonplaceholder.typicode.com/posts/2',{
      title: 'foowa',
   });
}

updateSingleData();

Screenshot 2022-12-18 210257

DELETE

const makeRequest = function(method,url,data1){
   const xhr = new XMLHttpRequest();
   xhr.open(method,url);
   xhr.setRequestHeader('Content-type','application/json');
   xhr.onload = function(){
      let data = xhr.response;
      console.log(JSON.parse(data));
   };
   xhr.onerror = function(){
      console.log('error is here');
   }
   xhr.send(JSON.stringify(data1));  
}

// delete 
// single data update
const deleteData = function(){
   makeRequest('DELETE','https://jsonplaceholder.typicode.com/posts/1');
}

deleteData();

Screenshot 2022-12-18 213336





fetch

  • fetch() has replace XMLHttpRequest
  • fetch() - global method for making HTTP request
  • 2 way to call -than, async await





    • fetch() is easy to use comapre to XMLHttpRequest
    • fetch() returns a promise
    • returned promise can only handle network error
    • does not support all the older browser

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published