This question already has answers here:
Using cURL to upload POST data with files
(11 answers)
Closed 2 months ago.
I send a json file by curl with this command and its work :
curl -X POST -H "Content-Type: application/json" -d #/Users/test/testjson.json 'http://127.0.0.1:5000
I collect this data like and its work :
#app.route('/json_test', methods=['POST'])
def process_data():
# Collect the JSON file from the request
data = json.loads(request.data)
#
I want to add both parameters source and id.
How can I send these parameters in Curl plus my json file . I tried
curl -i -X POST -H "Content-Type: multipart/form-data"
-F "data /Users/test/testjson.json” -F "source=Value1" -F "ident=Value2" http://127.0.0.1:5000
But it's not working
if you can help me to have the command and how i can read this data with python.
Please provide more information of the content on both values you want to add.
I changed a quotation mark inside your curl, try with:
curl -i -X POST -H 'Content-Type: multipart/form-data'
-F 'data /Users/test/testjson.json' -F "source=Value1" -F 'ident=Value2' http://127.0.0.1:5000
To use variables from the curl, you need to update your method. The below curl is another example you can try for now:
#app.route('/json_test', methods=['POST'])
def process_data(source: str, ident: str):
# Collect the JSON file from the request
data = json.loads(request.data)
#
Curl Command:
curl -i -X POST \
'http://127.0.0.1:5000/json_test/?source=value1/?ident=value2' \
-H 'Content-Type: multipart/form-data' \
-F 'data /Users/test/testjson.json'
Try this:
curl -i -X POST -H "Content-Type: multipart/form-data"
-F "user_input=</Users/test/testjson.json” -F "source=Value1" -F "ident=Value2" http://127.0.0.1:5000/json_test
Note that this will create a form with three fields: user_input, source, and ident. You'll need to modify your controller endpoint as well:
#app.route('/json_test', methods=['POST'])
def process_data():
form_content_as_json = json.loads(request.form)
So I'm trying to parse this data into a csv or list (to manipulate with python). This clearly isn't a json format (from what I googled) that is returning.
Question: What is this format?
curl -H "Accept: application/json" -X POST -d 'plan=11' https:/example.com/rest/api/m_spl/search
{"COLUMNS":["Value1","Value2", "Value3", "Value4"],"DATA":[]}
"DATA":[["Data1","Data2\nData2a","","Data3"],…}
Thanks
I have been trying to run this code,
import csv
import os
token = 'Bearer xxx"'
with open('uris.csv', 'r', newline = '',encoding = 'utf-8') as ifp:
ir = csv.reader(ifp)
for i, row in enumerate(ir):
v = ('curl -X "POST" "https://api.spotify.com/v1/playlists/7miRhC7OZhQUvnP1ONghJm/tracks?uris=spotify%3Atrack%3A'+
', '.join(row)+
'" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: '+
token + '\n')
os.system("gnome-terminal -e 'bash -c \""+v+"; sleep 10\" '")
print(v)
which takes some spotify track uris from a csv and
puts them into the curl that anyone can get from Spotify Api. This curl adds the specific track to my playlist. Then I open a new terminal and execute the curl. (the os.system command was found from open terminal run command python)
The problem is that when I execute this code with python3 code.py new terminals for each curl open but none curl is executed. The curls themselves are right. If I copy paste one curl and run it, the track is added to my playlist. Also if I run a curl separately, I get a response in the terminal which i don't get if I run curls through the code. The response is something like this:
{
"snapshot_id" : "MTQxxx"
}
Thanks a lot.
Silly to ask, but why not just directly call curlOutput = os.system("curl [your concatenation]")?
Might just solve your issue.
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!
I am doing a development based on instagram, I do not use the API because they do not allow applications in development mode but in production mode.
So, I'm trying to get the following pages regarding a hashtag, for example:
https://www.instagram.com/explore/tags/plebiscito/
The next page is done by sending a POST method, to a /query / and a /ajax/bz, however, trying to try it with cURL does not work for me.
I leave as I have been doing with cURL.
curl "https://www.instagram.com/explore/tags/plebiscito/" --http1.1 -k "https://www.instagram.com/query/" -H "Content-Type: application/x-www-form-urlencoded" -X POST -d "q=ig_hashtag(plebiscito)+{+media.after(j0hwe66aaaaaf0hwexjmwaaafkwa,+12)+{++count,++nodes+{++++caption,++++code,++++comments+{++++++count++++},++++comments_disabled,++++date,++++dimensions+{++++++height,++++++width++++},++++display_src,++++id,++++is_video,++++likes+{++++++count++++},++++owner+{++++++id++++},++++thumbnail_src,++++video_views++},++page_info}+}&ref=tags::show&query_id=/" --next --http1.1 -k "https://www.instagram.com/ajax/bz" -H "Content-Type: application/json" -X POST -d '{"q":[{"page_id":"7mj51x","posts":[["timespent_bit_array",{"tos_id":"7mj51x","start_time":1481556875,"tos_array":[3,0],"tos_len":2,"tos_seq":2,"tos_cum":19,"log_time":1481556876912},1481556876912,0]],"trigger":"timespent_bit_array"}],"ts":1481556877336}' --next -k "https://www.instagram.com/query/" -H "Content-Type: application/x-www-form-urlencoded" -X POST -d "q=ig_hashtag(plebiscito)+{+media.after(j0hwe66aaaaaf0hwexjmwaaafkwa,+8)+{++count,++nodes+{++++caption,++++code,++++comments+{++++++count++++},++++comments_disabled,++++date,++++dimensions+{++++++height,++++++width++++},++++display_src,++++id,++++is_video,++++likes+{++++++count++++},++++owner+{++++++id++++},++++thumbnail_src,++++video_views++},++page_info}+}&ref=tags::show&query_id=/" --next -k "https://www.instagram.com/ajax/bz" -H "Content-Type: application/json" -X POST -d '{"q":[{"page_id":"7mj51x","posts":[["timespent_bit_array",{"tos_id":"7mj51x","start_time":1481556875,"tos_array":[3,0],"tos_len":2,"tos_seq":2,"tos_cum":19,"log_time":1481556876912},1481556876912,0]],"trigger":"timespent_bit_array"}],"ts":1481556877336}'
Here I was trying to get page two, however, it responds with a page not found.
In short, I need to automate the pages with Python, but I was testing it with cURL.
Could you help me? thank you very much.
First of all, you can get a json https://www.instagram.com/explore/tags/plebiscito/?__a=1 It contains page_info with end_cursor. Paginate using max_id=VALUE-FROM-end_cursor in GET-request. More info here.
If you want to use POST /query you need right cookie.