Files
apidash/packages/curl_parser
Udhay-Adithya 8036d60615 feat: harden curl_parser, tolerate flags, improve output, and expand tests
- Pre-filter unknown flags before ArgParser; keep positional args.
- Tolerate non-request flags: -v/--verbose, --connect-timeout, --retry, --output, --compressed, -i/--include, --globoff.
- Auth: support --oauth2-bearer; map to - Authorization only if absent.
- Cookies: parse -b/--cookie; accept -c/--cookie-jar (ignored for request).
- URL: prefer first http(s) positional when --url missing; quote cleaning.
- Data: merge data-urlencode → data-raw → data-binary → data; default POST when body/form present; HEAD remains HEAD.
- Forms: parse -F entries; auto-set multipart Content-Type if missing.
- Headers: robust -H parsing for multi-colon values.
- toCurlString: deterministic order; fix continuation spacing; emit -d right after headers/form; place -k/-L at end.
- Utils: normalize backslash-newlines/CRLF; remove stray '+'; shlex split.
- Tests: add unknown flags, oauth2-bearer (and non-override), cookie-jar, verbose/timeout/retry/output tolerance, data merging order, HEAD+data, -A user-agent, -b filename.
- Docs: add Dartdoc for utils; class docs present.
2025-09-17 00:11:20 +05:30
..
2024-11-24 14:56:31 +05:30
2025-01-11 14:23:00 +05:30
2025-01-11 14:23:00 +05:30
2024-11-24 06:01:04 +05:30
2025-05-25 14:10:14 +05:30
2024-11-24 16:28:42 +05:30
2025-05-25 14:10:14 +05:30
2024-11-24 16:37:37 +05:30

curl Parser

Pub Version

Easily parse a cURL command into a Dart object and generate cURL commands from Dart objects.
A well-tested and better alternative to curl_converter.

Features

  • Parse a cURL command into a Curl class instance.
  • Format a Curl object back into a cURL command.
  • Supports various options such as request method, headers, data, cookies, user-agent, and more.

Contribute

In case you would like to add a feature, feel free to raise an issue in our repo and send across a Pull Request.

Usage

  1. Add the package to your pubspec.yaml file

  2. Use the package:

Example 1: GET

import 'package:curl_parser/curl_parser.dart';

void main() {
  final curlGetStr = 'curl https://api.apidash.dev/';
  final curlGet = Curl.parse(curlGetStr);

  // Parsed data
  print(curlGet.method);
  // GET
  print(curlGet.uri);
  // https://api.apidash.dev/

  // Object to cURL command
  final formattedCurlGetStr = curlGet.toCurlString();
  print(formattedCurlGetStr);
  // curl "https://api.apidash.dev/"
}

Example 2: HEAD

import 'package:curl_parser/curl_parser.dart';

void main() {
  final curlHeadStr = 'curl -I https://api.apidash.dev/';
  final curlHead = Curl.parse(curlHeadStr);

  // Access parsed data
  print(curlHead.method);
  // HEAD
  print(curlHead.uri);
  // https://api.apidash.dev/

  // Object to cURL command
  final formattedCurlHeadStr = curlHead.toCurlString();
  print(formattedCurlHeadStr);
  // curl -I "https://api.apidash.dev/"
}

Example 3: GET + HEADERS

import 'package:curl_parser/curl_parser.dart';

void main() {
  final curlHeadersStr = 'curl -H "X-Header: Test" https://api.apidash.dev/';
  final curlHeader = Curl.parse(curlHeadersStr);

  // Access parsed data
  print(curlHeader.method);
  // GET
  print(curlHeader.uri);
  // https://api.apidash.dev/
  print(curlHeader.headers);
  // {"X-Header": "Test"}

  // Object to cURL command
  final formattedCurlHeaderStr = curlHeader.toCurlString();
  print(formattedCurlHeaderStr);
  // curl "https://api.apidash.dev/" \
  //  -H "X-Header: Test"
}

Example 4: POST

import 'package:curl_parser/curl_parser.dart';

void main() {
  final curlPostStr = r"""curl -X 'POST' \
  'https://api.apidash.dev/case/lower' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "text": "Grass is green"
}'""";
  final curlPost = Curl.parse(curlPostStr);

  // Access parsed data
  print(curlPost.method);
  // POST
  print(curlPost.uri);
  // https://api.apidash.dev/case/lower
  print(curlPost.headers);
  // {"accept": "application/json", "Content-Type": "application/json"}
  print(curlPost.data);
  // {
  //   "text": "Grass is green"
  // }

  // Object to cURL command
  final formattedCurlPostStr = curlPost.toCurlString();
  print(formattedCurlPostStr);
  // curl -X POST "https://api.apidash.dev/case/lower" \
  //  -H "accept: application/json" \
  //  -H "Content-Type: application/json" \
  //  -d '{
  //   "text": "Grass is green"
  // }'
}

Check out test for more examples.

Maintainer

License

This project is licensed under the Apache License 2.0.