Connecting to HTTP
With RapidQL, you can also connect to any HTTP url using Http.get(). You may also use patch, post, and put requests.
const RapidQL = require('RapidQL');
const rql = new RapidQL({});
rql.log(`{
Http.get(url:"http://httpbin.org/ip") {
origin
}
}`);
An HTTP request in RQL can take many parameters beyond the url. The params include:
- url: a fully qualified URI
- body: entity body for PATCH, POST and PUT requests (not usable on GET requests)
- form: data to pass for a multipart/form-data request (not usable on GET requests)
- json: a boolean that when true, sets body to JSON representation of value and adds the Content-type: application/json header (not usable on GET requests)
- headers: HTTP headers (default: {})
- bearer: an OAuth bearer token
- basic: credentials for basic auth.
const RapidQL = require('RapidQL');
const rql = new RapidQL({});
rql.log(`{
Http.get(
url:"http://httpbin.org/ip",
basic: {
username:"iddoGino",
password:"notGonnaPutMyPasswordInTheDocs"
}
){
origin
}
}`);
Setting Default HTTP Parameters
When initializing your RapidQL instance, you can provide default parameters for HTTP requests by supplying an Http object as an option. This Http can set default parameters for headers, bearer, and basic. These parameters will then be sent with every HTTP request.
const RapidQL = require('RapidQL');
const rql = new RapidQL({
Http: {
headers : {
'X-RapidAPI-Key' : '*****************',
'default' : 'this header will always be sent, unless defined otherwise at time of call'
},
basic : {
username : 'my_username',
password : 'my_password'
}
}
});
rql.log(`{
Http.post(
url:"http://httpbin.org/ip"
){
}
}`)
Overriding the default parameters
If you want to use different parameters other that your defaults, you can override them by providing them within the rql query. Parameters provided at the time of query will always take precedence. See example below:
const RapidQL = require('RapidQL');
const rql = new RapidQL({
Http: {
// In this example, this basic auth will not be sent
basic : {
username : 'my_username',
password : 'my_password'
}
}
});
rql.log(`{
Http.post(
url:"http://httpbin.org/ip",
basic : {
username : 'this_basic_auth_will_be_sent!',
password : 'my_password'
}
) {
}
}`)
Updated almost 5 years ago