Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (7k points)
I am working on a project that consumes REST service APIs and I do not know how to call them in JavaScript. Can anyone give me a basic example of different method calls in JavaScript or any of its frameworks?

1 Answer

0 votes
by (13.1k points)
edited by

Post method:

instancename.post(API url,object,{

headers:request headers}).then(response=>{

//posting the necessary data to do something here

}).catch(error=>console.error('error faced',error));

Get method:

instancename.get('${API url}/${thing you want}',{

headers: request headers}).then(response=>{

//code you want to do with response

}).catch(error=>console.error('error message',error))

PUT method:

instancename.put('${API url}/${thing you want}',object,{headers:request headers}).then(response=>{

//code action you want to perform

 }).catch(error=>console.error('PUT request error',error))

 Delete method:

 instance.delete('${API URL}/${thing you want}',{

 headers:request headers}).then(response=>{

 //something here

 }).catch(error=>console.error('Delete error',error))

These are just a basic syntax of how to implement them. i would recommend you to experiment and maybe you can make some tweaks to them.

Enroll in our web development course to become an expert in Web Development!

Related questions

+1 vote
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...