2

I am trying to convert a python POST requests to a curl statement for the following request:

# this is the requests.post I want to convert to CURL - it works for python but
# I need to run this in a shell script, so I need to convert the following to 
# curl statement:

response = requests.post(url,
                         files=files,
                         headers=headers)


# the "files" in the above request.post contain a json data AND 
# a yaml data as shown below:

files = {
    'json': (None, json.dumps(jsondata), 'application/json'),
    'file': ('heat_template', heat_yaml,'application/yaml')}

# However, in python, the 'file' class that contains the yaml data is assigned with cgi.FieldStorage class.  


# the header contains X-Auth-Token
headers = {}
headers['X-Auth-Token'] = token_value

Originally I tried to use the following curl statement but it doesn't work:

curl -i X POST -d $JSONDATA -H "Content-Type:application/json" -data-urlencode "file@datafile.yaml" -H "Content-Type:application/yaml" $url -H "X-Auth-Token:$TOKEN"

UPDATE: I motified the curl statement to the following and it worked 'partially':

curl -i -X POST -F json="$JSONDATA" -F file="$ENCODED_YAML" $URL -H "X-Auth-Token:$TOKEN"

The destination url $URL is able to translate the json data -F json="$JSONDATA" and the header data H "X-Auth-Token:$TOKEN") correctly from the curl statement, but the -F file="$ENCODED_YAML" is treated as a string python class instead of the expected cgi.FieldStorage python class. How do we pass a file data as a cgi.FieldStorage class in a curl statement?

Appreciate the help!

punsoca
  • 459
  • 1
  • 7
  • 15
  • 1
    See https://stackoverflow.com/questions/7172784/how-do-i-post-json-data-with-curl-from-a-terminal-commandline-to-test-spring-res – Vikash Balasubramanian Dec 04 '19 at 02:13
  • What have you tried so far? Do you have questions about the `curl` documentation? – larsks Dec 04 '19 at 02:38
  • @larsks thank you for looking - I updated the above post with the curl statement I tried (but didnt work, of course lol) – punsoca Dec 04 '19 at 03:43
  • Does the curl statement give an error? What happens when you execute it? – Sanil Khurana Dec 04 '19 at 03:58
  • 1
    There are a number of basic errors in that command line that a close reading of the `curl` man page should fix. E.g., `X` should be `-X`, and long options require a `--` rather than `-`, etc. – larsks Dec 04 '19 at 04:24
  • @larsks, I updated the curl statement containing two -F parameters (one is a JSON data and another is a YAML data). The destination URL is able to translate the json data and the headers correctly, but not the yaml data. Would like to know how do we convert the yamldata as a cgi.FieldStorage data? Appreciate it! – punsoca Dec 12 '19 at 06:09

0 Answers0