If you’re coding tools that use APIs or other online services to retrieve or send data, I imagine you’d love to delve into their documentation. Typically, there are code samples that explain how to ‘chat’ with the API, and these examples most often use the curl command. When you’re lucky, there are also examples in Python or JS or even JavaScript, but that doesn’t go any further.
So, how do you save time if you need to write R, Go, C#, Ruby, Rust, Elixir, Java, MATLAB, Dart, CFML, Ansible URI, or JSON?
Well, with the CurlConverter website, you’ll be able to convert all these curl commands into the language of your choice. It is also available with the curlconverter command that you can install on your machine.
For example, this authentication curl command:
curl "https://example.com/" -u "some_username:some_password"
Becomes the following code in DART:
import 'dart:convert';
import 'package:http/http.dart' as http;
void main() async {
var uname = 'some_username';
var pword = 'some_password';
var authn = 'Basic ' + base64Encode(utf8.encode('$uname:$pword'));
var url = Uri.parse('https://example.com/');
var res = await http.get(url, headers: {'Authorization': authn});
if (res.statusCode != 200) throw Exception('http.get error: statusCode= ${res.statusCode}');
print(res.body);
}
Or in C#:
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://example.com/");
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("some_username:some_password")));
HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
CurlConverter can interpret Bash syntax, such as ANSI-C strings and piped files. It also knows all the curl arguments (there are over 300 of them!!), including those that have been removed from recent versions (and therefore ignored most of the time). It can also convert JSON data into native objects and generate code that can read it either from a file or an input stream.
Unfortunately, only HTTP is supported, and code generators for other languages are less comprehensive than the generator for Python.
To use curlconverter, you can install it as a JavaScript library with the
npm install curlconverter
Or as a command-line tool like this:
npm install --global curlconverter
Here’s an example of the code generated from this command:
$ curlconverter --data "hello=world" example.com
import requests
data = {
'hello': 'world',
}
response = requests.post('http://example.com', data=data)
In short, super convenient for those who want to simplify the process of creating HTTP requests in their favorite language while relying on more familiar curl commands.”