Skip to content

Bug: curl syntax for JSON -d should escape single quote #1068

Open
@yungyuchen521

Description

@yungyuchen521

Description

The generated curl sample code doesn't escape single quote for JSON body.

Reproduction

This can be reproduced with the OpenAPI spec below:

{
  "openapi": "3.0.0",
  "paths": {
    "/": {
      "post": {
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "text": {
                    "type": "string"
                  }
                }
              },
              "examples": {
                "Message": {
                  "value": {
                    "text": "The user's message"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

The corresponding curl syntax doesn't escape ' for -d:

curl -X POST "http://localhost:3000/" \
 -H "content-type: application/json" \
 -d '{"text":"The user's message"}'

The correct syntax is:

curl -X POST "http://localhost:3000/" \
 -H "content-type: application/json" \
 -d '{"text":"The user'\''s message"}'

Suggestion

Change this line to:

curlData = ` -d '${JSON.stringify(JSON.parse(exampleTextAreaEl.value.replace(/'/g, "'\\''")))}' \\\n`;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions