- Meteor - Best Practices
- Meteor - ToDo App
- Meteor - Running on mobile
- Meteor - Deployment
- Meteor - Structure
- Meteor - Publish & Subscribe
- Meteor - Package.js
- Meteor - Methods
- Meteor - Accounts
- Meteor - Sorting
- Meteor - Security
- Meteor - Assets
- Meteor - Email
- Meteor - HTTP
- Meteor - EJSON
- Meteor - Timers
- Meteor - Blaze
- Meteor - Check
- Meteor - Core API
- Meteor - Packages
- Meteor - Tracker
- Meteor - Session
- Meteor - Events
- Meteor - Forms
- Meteor - Collections
- Meteor - Templates
- Meteor - First Application
- Meteor - Environment Setup
- Meteor - Overview
- Meteor - Home
Meteor Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Meteor - HTTP
This package provides HTTP request API with get, post, put and delete methods.
Install Package
We will install this package by running the following code in the command prompt window.
C:UsersusernameDesktopmeteorApp>meteor add http
CALL Method
This is universal method that can use GET, POST, PUT and DELETE arguments. The following example demonstrates how to use GET argument. The examples in this chapter will use fake REST API from
.You can see that this method is using four arguments. We already mentioned the first argument GET. The second one is API URL. The third argument is an empty object, where we can set some optional parameters. The last method is an asynchronous callback, where we can handle errors and work with a response.
HTTP.call( GET , http://jsonplaceholder.typicode.com/posts/1 , {}, function( error, response ) { if (error) { console.log(error); } else { console.log(response); } });
GET Method
The same request can be sent using GET instead of CALL method. You can see that the first argument now is API URL.
HTTP.get( http://jsonplaceholder.typicode.com/posts/1 , {}, function( error, response ) { if ( error ) { console.log( error ); } else { console.log( response ); } });
Both of the previous examples will log the same output.
![Meteor HTTP Call](/meteor/images/meteor-http-call.jpg)
POST Method
In this method, we are setting data that needs to be sent to the server (postData) as the second argument. Everything else is the same as in our GET request.
var postData = { data: { "name1": "Value1", "name2": "Value2", } } HTTP.post( http://jsonplaceholder.typicode.com/posts , postData, function( error, response ) { if ( error ) { console.log( error ); } else { console.log( response); } });
The console will log our postData object.
![Meteor HTTP Post](/meteor/images/meteor-http-post.jpg)
PUT Method
We can update our data using the PUT method. The concept is the same as in our last example.
var updateData = { data: { "updatedName1": "updatedValue1", "UpdatedName2": "updatedValue2", } } HTTP.put( http://jsonplaceholder.typicode.com/posts/1 , updateData, function( error, response ) { if ( error ) { console.log( error ); } else { console.log( response ); } });
Now, we can see our updated object in the console.
![Meteor HTTP Put](/meteor/images/meteor-http-put.jpg)
DEL Method
We can send a delete request to the server using the DEL method. We will delete everything inside the data object.
var deleteData = { data: {} } HTTP.del( http://jsonplaceholder.typicode.com/posts/1 , deleteData, function( error, response ) { if ( error ) { console.log( error ); } else { console.log( response ); } });
The console will show that the deleting process is successful.
![Meteor HTTP Del](/meteor/images/meteor-http-del.jpg)