skip to Main Content

Given the –data parameter of a curl statement:

--data '{
    "collection": [
        "Library"
    ],
    "filter": "{'''target.endpointUID''':{'''$eq''': '''ABC123'''}}",
    "skip": 0,
    "limit": 16
}'

I’d like to replace ABC123 with the variable I want to use but I cannot figure out the correct way.

2

Answers


  1. You can use here-document (this assumes you don’t have special characters like double quotes in $var):

    var=ABC123
    curl ... --data "$(cat << EOF
    {
        "collection": [
            "Library"
        ],
        "filter": "{'target.endpointUID':{'$eq': '$var'}}",
        "skip": 0,
        "limit": 16
    }
    EOF
    )" ...
    
    Login or Signup to reply.
  2. You want

    data=$(
      jq -nc --arg uid "${your_variable}" '
        {
          "collection": ["Library"],
          "filter": "{"target.endpointUID":{"$eq": "($uid)"}}",
          "skip": 0,
          "limit": 16
        }
      '
    )
    
    curl --data "$data" ...
    

    or, a bit clearer

    data=$(
      jq -nc --arg uid "${your_variable}" '
        {"target.endpointUID": {"$eq": $uid}} as $filter
        | {collection: ["Library"], filter: ($filter | tojson), skip: 0, limit: 16}
      '
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search