简体   繁体   中英

Pass json value to curl variable via CLI within Bash script

Using GET I need to pass a json value to a URL via the command line within a bash script.

This works:

curl -i "http://MYURL:8080/admin/rest_api/api?api=trigger_dag&dag_id=spark_submit&conf=\{\"filename\":\"myfile.csv\"\}"

If I want to expand on the json value, I would prefer to pass a variable via the URL parameter for readability. Somethig like ... but this doesn't appear to work correctly.

generate_post_data =
{
  "filename": "myfile.csv"    
}

curl -i "http://MYURL:8080/admin/rest_api/api?api=trigger_dag&dag_id=spark_submit&conf=${generate_post_data}"

You need to properly set the variable and you should url encode it using the --data-urlencode option.

#!/bin/bash

generate_post_data="filename=myfile.csv"
curl -G "http://MYURL:8080/admin/rest_api/api?api=trigger_dag&dag_id=spark_submit" --data-urlencode $generate_post_data

From the manpage:

--data-urlencode <data>

(HTTP) This posts data, similar to the other -d, --data options with the exception that this performs URL-encoding.

To be CGI-compliant, the part should begin with a name followed by a separator and a content specification. The part can be passed to curl using one of the following syntaxes:

For more info you can use man curl and then /data-urlencode to jump to the section on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM