Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
767 views
in Technique[技术] by (71.8m points)

api - Test flask rest using curl. GET request with multiple query parameters

I'm using Flask framework to create the rest API. the code below is the rest I've wrote.

@app.route("/posts/v2/list_by_date", methods=['GET'])
def public_posts_list_by_date():
    tracker_id = request.args.get('tracker_id')
    advanced_filters = request.args.get('filters')


    if advanced_filters:
        advanced_filters = json.loads(advanced_filters)
    else:
        advanced_filters = {}

the 'filters' is an array of parameters such as start_date, limit, offset, end_date.

And I want to test the rest and I used this command to test it:

curl http://172.31.32.22:3100/posts/v2/list_by_date?tracker_id=6939&filters=start_date=2020-11-30T15:05:05&end_date=2021-01-20T15:05:05

The problem is I don't receive the parameters of the filters in flask, it always empty. I tried the above command and many other curl commands But still, it doesn't send the filters parameters.

question from:https://stackoverflow.com/questions/65907824/test-flask-rest-using-curl-get-request-with-multiple-query-parameters

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the parameter --data-urlencode to encode the data beforehand.
See also here.

curl -G "http://172.31.32.22:3100/posts/v2/list_by_date" 
     --data-urlencode 'tracker_id=6939' 
     --data-urlencode 'filters={"start_date":"2020-11-30T15:05:05", "end_date":"2021-01-20T15:05:05"}'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...