Skip to content

Instantly share code, notes, and snippets.

@redeemefy
Created September 23, 2020 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redeemefy/56b35e760d49ebed2cb55ec8bd141048 to your computer and use it in GitHub Desktop.
Save redeemefy/56b35e760d49ebed2cb55ec8bd141048 to your computer and use it in GitHub Desktop.
ElasticSearch REST client that allows body in GET requests
const https = require("https")
const http = require("http")
const fetch = require("node-fetch")
const ElasticSearchRestClient = {
get: function(body, options = {}) {
return new Promise((resolve, reject) => {
const callback = function(response) {
let str = ""
response.on("data", function(chunk) {
str += chunk
})
response.on("end", function() {
resolve(JSON.parse(str))
})
}
const req = (options.protocol === "https:" ? https : http).request(options, callback)
req.on("error", err => {
reject(err)
})
if(body !== null) {
req.write(body)
}
req.end()
})
},
post: async function(url, options, body) {
// const url = `${options.protocol}//${options.host}:${options.port}${options.path}`
const response = await fetch(url, { method: "POST", headers: options.headers, body: body })
return await response.json()
}
}
module.exports = { ElasticSearchRestClient }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment