### Install curl-parser using npm Source: https://github.com/scrape-do/curl-parser/blob/master/README.md This command installs the curl-parser module as a dependency for your project using npm. ```sh npm i -S @scrape-do/curl-parser ``` -------------------------------- ### Handle Curl Parsing Errors with TypeScript Source: https://context7.com/scrape-do/curl-parser/llms.txt Illustrates how to use try-catch blocks to gracefully handle errors thrown by the parse function for invalid or unrecognized curl commands. Includes examples for common errors and a safe parsing wrapper function. ```typescript import { parse } from "@scrape-do/curl-parser"; // Error: Invalid command (must start with 'curl') try { parse("wget https://example.com"); } catch (error) { console.error(error.message); // "Invalid command: wget" } // Error: Unrecognized long option try { parse("curl --unknown-option https://example.com"); } catch (error) { console.error(error.message); // "Unrecognized argumen: --unknown-option" } // Error: Unrecognized short option try { parse("curl -z https://example.com"); } catch (error) { console.error(error.message); // "Unrecognized option: z in \"-z\"" } // Error: Invalid header format try { parse("curl -H 'invalid header without colon' https://example.com"); } catch (error) { console.error(error.message); // "Invalid header value: invalid header without colon" } // Error: Unmatched quotes try { parse("curl -H 'unclosed quote https://example.com"); } catch (error) { console.error(error.message); // "Unmatched quote: ..." } // Safe parsing wrapper function safeParse(command: string) { try { return { success: true, data: parse(command) }; } catch (error) { return { success: false, error: error.message }; } } const result = safeParse("curl https://valid.example.com"); if (result.success) { console.log("Parsed URL:", result.data.url); } ``` -------------------------------- ### Parse curl command string to object (TypeScript) Source: https://context7.com/scrape-do/curl-parser/llms.txt Parses a curl command string into a structured CurlCommand object. It tokenizes the input, extracts URL, method, headers, body, cookies, and flags. Handles various curl command formats, including simple GET, POST with data, combined flags, cookies, and Chrome DevTools exports. ```typescript import { parse } from "@scrape-do/curl-parser"; // Parse a simple GET request const simpleCmd = parse("curl https://api.example.com/users"); console.log(simpleCmd); // Output: { // url: "https://api.example.com/users", // method: "get", // headers: [], // body: null, // bodyArg: null, // cookies: null, // flags: {} // } // Parse a POST request with headers and JSON body const postCmd = parse(`curl -X POST \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer token123' \ -d '{\"name\":\"John\",\"email\":\"john@example.com\"}' \ https://api.example.com/users`); console.log(postCmd.method); // "POST" console.log(postCmd.url); // "https://api.example.com/users" console.log(postCmd.headers); // [{ key: "Content-Type", value: "application/json" }, { key: "Authorization", value: "Bearer token123" }] console.log(postCmd.body); // '{"name":"John","email":"john@example.com"}' console.log(postCmd.bodyArg); // "data" // Parse command with multiple combined flags (e.g., -fsSL from install scripts) const flagsCmd = parse("curl -fsSL https://bun.sh/install"); console.log(flagsCmd.flags); // Output: { fail: true, silent: true, showError: true } // Parse command with cookies const cookieCmd = parse("curl -b 'session=abc123; user=john' https://example.com"); console.log(cookieCmd.cookies); // "session=abc123; user=john" // Parse complex Chrome DevTools export const chromeCmd = parse(`curl 'https://api.example.com/data' \ -H 'accept: application/json' \ -H 'user-agent: Mozilla/5.0' \ -b 'tracking_id=xyz789'`); console.log(chromeCmd.url); // "https://api.example.com/data" console.log(chromeCmd.headers.length); // 2 console.log(chromeCmd.cookies); // "tracking_id=xyz789" ``` -------------------------------- ### Parse and Stringify Curl Commands in TypeScript Source: https://github.com/scrape-do/curl-parser/blob/master/README.md Demonstrates how to use the `parse` and `stringify` functions from the curl-parser module to convert a curl command string into a JavaScript object and then back into a string. Requires importing the functions from the '@scrape-do/curl-parser' package. ```ts import { parse, stringify } from "@scrape-do/curl-parser"; const command = parse( "curl -X POST -H x-foo:bar -X baz:zap https://httpbin.org" ); console.log("serialized:", stringify(command)); ``` -------------------------------- ### Parse Curl Data Options with TypeScript Source: https://context7.com/scrape-do/curl-parser/llms.txt Demonstrates how to use the parse function from @scrape-do/curl-parser to handle different curl data options (--data, --data-ascii, --data-binary, --data-raw, --data-urlencode). Each option correctly sets the body and bodyArg properties in the parsed command. ```typescript import { parse } from "@scrape-do/curl-parser"; // Standard --data / -d option const dataCmd = parse("curl --data 'key=value&foo=bar' https://api.example.com"); console.log(dataCmd.body); // "key=value&foo=bar" console.log(dataCmd.bodyArg); // "data" // Binary data (often used for file uploads) const binaryCmd = parse("curl --data-binary @./upload.zip https://api.example.com/upload"); console.log(binaryCmd.body); // "@./upload.zip" console.log(binaryCmd.bodyArg); // "binary" // ASCII data const asciiCmd = parse("curl --data-ascii 'plain text content' https://api.example.com"); console.log(asciiCmd.bodyArg); // "ascii" // Raw data (no processing) const rawCmd = parse("curl --data-raw '{\"already\":\"encoded\"}' https://api.example.com"); console.log(rawCmd.bodyArg); // "raw" // URL-encoded data with multiple parameters (concatenated with &) const urlencodeCmd = parse(`curl \ --data-urlencode 'name=John Doe' \ --data-urlencode 'email=john@example.com' \ --data-urlencode 'message=Hello World!' \ https://api.example.com/form`); console.log(urlencodeCmd.body); // "name=John Doe&email=john@example.com&message=Hello World!" console.log(urlencodeCmd.bodyArg); // "urlencode" // URL-encode with = prefix (value-only encoding) const valueOnlyCmd = parse("curl --data-urlencode '=encodethis' https://api.example.com"); console.log(valueOnlyCmd.body); // "encodethis=" ``` -------------------------------- ### Handle Multiple --data-urlencode Parameters Source: https://github.com/scrape-do/curl-parser/blob/master/README.md Illustrates how the curl-parser module handles multiple `--data-urlencode` parameters by concatenating their values with an ampersand (&). This is useful for constructing request bodies with multiple URL-encoded key-value pairs. ```sh curl -X POST --data-urlencode foo=bar --data-urlencode bar=zap https://example.com ``` -------------------------------- ### Inspect Parsed CurlCommand Object with TypeScript Source: https://context7.com/scrape-do/curl-parser/llms.txt Shows how to parse a complex curl command and inspect all properties of the returned CurlCommand object, including URL, method, headers, body, cookies, and flags. ```typescript interface CurlCommand { url: string | null; headers: { key: string; value: string }[]; body: string | null; bodyArg: "data" | "ascii" | "binary" | "raw" | "urlencode" | null; method: string; flags: CurlCommandFlags; cookies: string | null; } interface CurlCommandFlags { anyauth?: boolean; // --anyauth basic?: boolean; // --basic compressed?: boolean; // --compressed crlf?: boolean; // --crlf compressedSsh?: boolean; // --compressed-ssh fail?: boolean; // -f, --fail globoff?: boolean; // -g, --globoff showError?: boolean; // -S, --show-error silent?: boolean; // -s, --silent } // Example: Inspecting all properties import { parse } from "@scrape-do/curl-parser"; const cmd = parse(`curl -fsSL \ -X PUT \ -H 'Content-Type: application/json' \ -H 'X-Request-ID: req-12345' \ -b 'auth=token' \ --compressed \ -d '{\"status\":\"active\"}' \ https://api.example.com/resource/42`); console.log("URL:", cmd.url); // "https://api.example.com/resource/42" console.log("Method:", cmd.method); // "PUT" console.log("Headers:", cmd.headers); // [{ key: "Content-Type", value: "application/json" }, { key: "X-Request-ID", value: "req-12345" }] console.log("Body:", cmd.body); // '{"status":"active"}' console.log("Body Arg:", cmd.bodyArg); // "data" console.log("Cookies:", cmd.cookies); // "auth=token" console.log("Flags:", cmd.flags); // { fail: true, silent: true, showError: true, compressed: true } ``` -------------------------------- ### Serialize CurlCommand object to string (TypeScript) Source: https://context7.com/scrape-do/curl-parser/llms.txt Serializes a CurlCommand object back into a curl command string. It properly escapes special characters for safe shell execution and constructs the command with appropriate flags and arguments. Supports round-trip conversion from parsed objects back to command strings. ```typescript import { parse, stringify } from "@scrape-do/curl-parser"; // Create a command object and serialize it const cmd = { url: "https://api.example.com/users", method: "post", headers: [ { key: "Content-Type", value: "application/json" }, { key: "Authorization", value: "Bearer mytoken" } ], body: '{"username":"john"}', bodyArg: "data" as const, cookies: null, flags: {} }; const curlString = stringify(cmd); console.log(curlString); // Output: curl -X post -H 'Content-Type:application/json' -H 'Authorization:Bearer mytoken' -d {\"username\":\"john\"} https://api.example.com/users // Round-trip: parse and re-serialize const original = "curl -X POST -H 'x-api-key:secret123' https://api.example.com"; const parsed = parse(original); const serialized = stringify(parsed as any); console.log(serialized); // Output: curl -X post -H 'x-api-key:secret123' https://api.example.com ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.