collect others parameters in my flask api [duplicate] - python

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)

Related

uploading flie to FastAPI endpoint using curl - 307 Temporary Redirect

I have a fastAPI endpoint that recieves a file and saves it to disk as follows:
from fastapi import FastAPI, File, UploadFile
import shutil
app = FastAPI()
#app.post('/upload')
async def upload_file(file: UploadFile=File(...)):
with open(file.filename, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
return {
"filename": file.filename,
}
This works as expected when I upload a file through the docs interface at http://localhost:8000/docs
I am able to select a file and it successfully uploads.
However, attempting the same with curl fail:
curl -X POST localhost:8000/upload -F file=#photo.png
the curl command returns nothing and on the server side a 307 Temporary Redirect is logged.
I am not sure what I am missing here
In some scenario/setup where FastAPI redirect the request, use the curl with full dns/ip address.
something like this :
curl -X 'POST' '127.0.0.1:8000/upload' -F 'file=#photo.png
or can add headers(-H) too based on the application that is built.
curl -X 'POST' \
'http://127.0.0.1:8000/upload' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F 'file=#photo.png;type=application/json'

Open Terminal and Run Curl in Python

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.

Converting python requests.post to CURL statement

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!

Curl Response in new line on terminal [duplicate]

This question already has answers here:
Automatically add newline at end of curl response body
(5 answers)
Closed 5 years ago.
I have AWS Lambda function , whom for example returns "hello World" , Then I execute my code:
curl -H "Content-Type: application/json" -X POST https://xxxxxx.execute-api.eu-west-1.amazonaws.com/statusok/xxxxxx -d #keyValue.json
I get response in terminal:
"Hello World!"andrejka#root:~/Desktop/Darbas/KeyValue$
The question is how i can change my code or execution script to get response in new line, like :
"Hello World!"
andrejka#root:~/Desktop/Darbas/KeyValue$
I already try /n but nothing. Please suggest!
Probably the easiest solution would be simply executing an echo statement after your curl-call like:
curl -H "Content-Type: application/json" \
-X POST https://xxxxxx.execute-api.eu-west-1.amazonaws.com/statusok/xxxxxx \
-d #keyValue.json; echo

Instagram pagination tag

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.

Categories