mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 02:39:19 +08:00
- 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.
curl Parser
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
Curlclass instance. - Format a
Curlobject 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
-
Add the package to your
pubspec.yamlfile -
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.