Skip to content

Instantly share code, notes, and snippets.

@jchadwick
Last active October 9, 2019 14:42
Show Gist options
  • Save jchadwick/8d8115f0a72b0f0302e8f1c9d4e822ad to your computer and use it in GitHub Desktop.
Save jchadwick/8d8115f0a72b0f0302e8f1c9d4e822ad to your computer and use it in GitHub Desktop.
String template -> KY request
const parser = require("http-message-parser");
const ky = require("ky-universal");
const targetUrl = "https://postman-echo.com";
function executeHttp(strings) {
const rawRequest = parser(strings.join("\n"));
const {
url,
body,
headers: { Host, ...headers },
method
} = rawRequest;
return ky(`${targetUrl}${url}`, { body, headers, method });
}
(async function example() {
const response = await executeHttp`
POST /post HTTP/1.1
Host: postman-echo.com
User-Agent: PostmanRuntime/7.15.2
Accept: */*
Cache-Control: no-cache
Postman-Token: 40254981-c5ff-4bc9-a358-b3177c6cf6ff,4d77d7fa-8de1-402d-8646-25665a39d734
Cookie: sails.sid=s%3A5jBX82jn3FOxkLAc0iua3LAQy1xQLO8H.ST%2Bn5rSQaG6lhfla9cT%2FE9q7ZCBHp4tXVi7fVg5Il8g
Accept-Encoding: gzip, deflate
Content-Length:
Connection: keep-alive
cache-control: no-cache
test=1&test=2
`;
console.log(response);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment