Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.