Skip to content

Instantly share code, notes, and snippets.

@sunnygleason
Created April 21, 2017 21:01
Show Gist options
  • Save sunnygleason/ceaab61620e7b6cb89de76f8b714d1de to your computer and use it in GitHub Desktop.
Save sunnygleason/ceaab61620e7b6cb89de76f8b714d1de to your computer and use it in GitHub Desktop.
Advanced BLOCKS Techniques - XHR BLOCK
export default (request) => {
const xhr = require('xhr');
const query = require('codec/query_string'); // for HTTP URL encoding
const auth = require('codec/auth'); // for HTTP Basic Auth
const base64 = require('codec/base64'); // for other custom schemes
const apiUrl = "https://api.duckduckgo.com/?";
var queryString = query.stringify({
q : request.message.text,
format : 'json'
});
var httpOptions = {
method : 'GET', // allowed GET/POST/PUT/OPTIONS/DELETE/PATCH
timeout : 5000, // max 1000 (10s)
headers : {
'Accept' : "application/json"
// 'Content-Type' : ...
// 'Authorization' : ...
// 'If-Modified-Since' : ...
},
body : null, // form body, JSON string, XML ...
};
if (request.message && request.message.text) {
return xhr.fetch(apiUrl + queryString, httpOptions).then((result) => {
request.message.search_results = JSON.parse(result.body);
return request.ok();
});
}
return request.ok();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment