Created
July 9, 2020 14:52
-
-
Save evanrs/a52eda8d3abbd4ce674f9615dbfff706 to your computer and use it in GitHub Desktop.
Convert to HTTPie request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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