Skip to content

Instantly share code, notes, and snippets.

@evanrs
Created July 9, 2020 14:52
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 evanrs/a52eda8d3abbd4ce674f9615dbfff706 to your computer and use it in GitHub Desktop.
Save evanrs/a52eda8d3abbd4ce674f9615dbfff706 to your computer and use it in GitHub Desktop.
Convert to HTTPie request
import { map } from "lodash";
export type HTTPieOptions = {
method: string;
headers?: Record<string, string>;
body?: string;
data?: unknown[] | Record<string, unknown>;
};
export function toHTTPie(url: string, { method, ...options }: HTTPieOptions) {
const headers = map(options.headers, (header, key) => `${key}:"${header}"`);
const data = options.data ?? JSON.parse(options.body);
const body = map(data, (value, key) => `${key}:='${JSON.stringify(value)}'`);
return `http ${method ?? "GET"} ${url} ${headers.join(" ")} ${body.join(" ")} `;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment