Skip to content

Instantly share code, notes, and snippets.

@pwright
Created November 18, 2024 12:30
Show Gist options
  • Save pwright/210d55239900d3d2e64843707fb40861 to your computer and use it in GitHub Desktop.
Save pwright/210d55239900d3d2e64843707fb40861 to your computer and use it in GitHub Desktop.
Creating curl commands for docs

Generating cURL Commands Using VSCode and the REST Client Extension

Manually crafting cURL commands can be tedious and error-prone, especially for complex HTTP requests. A more efficient method involves using Visual Studio Code (VSCode) with the REST Client extension. This approach allows you to easily set up, test, and validate your HTTP requests before converting them into cURL commands. Here's how you can do it:

Prerequisites

  • Visual Studio Code installed on your computer.

  • REST Client Extension by Huachao Mao installed in VSCode. You can download it from the VSCode Marketplace .

Steps to Create and Export cURL Commands

1. Set Up Your HTTP Request

Open VSCode and create a new file with a .http or .rest extension. Write your HTTP request in a simple and readable format. Here's an example:

POST http://localhost:8080/apis/registry/v3/groups/my-group/artifacts HTTP/1.1
Content-Type: application/json

{
  "artifactId": "mytopic-value",
  "artifactType": "AVRO",
  "name": "Invoice",
  "description": "A standard Acme invoice payload.",
  "labels": {
    "label-1": "value-1",
    "label-2": "value-2"
  },
  "firstVersion": {
    "version": "1.0.0",
    "content": {
      "content": "{\"type\":\"record\",\"name\":\"ExampleType\",\"fields\":[{\"name\":\"sdfgfsdgsdg\",\"type\":\"string\"}]}",
      "contentType": "application/json",
      "references": []
    },
    "name": "ExampleType",
    "description": "A simple example of an Avro type.",
    "labels": {}
  }
}

2. Test and Validate the Request

  • Send the Request: Hover over the HTTP request and click on the "Send Request" button that appears above it.

  • View the Response: The REST Client will execute the request and display the response in a new pane. This allows you to verify that the request is functioning as intended.

  • Make Adjustments: If necessary, modify the request based on the response received to ensure accuracy.

3. Copy the Request as a cURL Command

  • Right-Click the Request: Within the HTTP request you've written, right-click to open the context menu.

  • Select 'Copy Request as cURL': Choose this option to copy the equivalent cURL command to your clipboard.

4. Use the cURL Command

curl --request POST \
  --url http://localhost:8080/apis/registry/v3/groups/my-group/artifacts \
  --header 'content-type: application/json' \
  --header 'user-agent: vscode-restclient' \
  --data '{"artifactId": "mytopic-value","artifactType": "AVRO","name": "Invoice","description": "A standard Acme invoice payload.","labels": {"label-1": "value-1","label-2": "value-2"},"firstVersion": {"version": "1.0.0","content": {"content": "{\"type\":\"record\",\"name\":\"ExampleType\",\"fields\":[{\"name\":\"sdfgfsdgsdg\",\"type\":\"string\"}]}","contentType": "application/json","references": []},"name": "ExampleType","description": "A simple example of an Avro type.","labels": {}}}'

Benefits of This Technique

  • Simplicity: Writing requests in the REST Client format is more straightforward than constructing cURL commands manually.

  • Validation: Testing requests before exporting ensures that the cURL commands will work as expected.

  • Efficiency: Quickly generate accurate cURL commands without worrying about syntax errors.

  • Documentation: Easily share complex requests in a widely accepted format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment