0

I have to call a Python Flask API, and pass variable arguments to the API. Below is my API route :

@app.route('/v2/products/category', methods = ['GET'])
def get_price_dif():
    page = request.args.get('page') 
    category = request.args.get('category')

Below is how I call the API with the arguments :

localhost:5000/v2/products/category?page=1&category=Mobiles & Accessories

The problem here is the API is not accepting the blank spaces in between the words "Mobiles", "&" and "Accessories" in the string "Mobiles & Accessories"

If I print the 'category' value received by the API, it shows only "Mobiles"

How do I call the API along with spaces in the category value?

2 Answers2

2

I think the space is not required when defining keyword arguments change your code to the code below. Remove white between methods argument and the ['GET'] array

@app.route('/v2/products/category', methods=['GET'])
def get_price_dif():
    page = request.args.get('page') 
    category = request.args.get('category')

Try replacing the spaces with %20 and see whether it will work

localhost:5000/v2/products/category?page=1&category=Mobiles%20&%20Accessories
Jjagwe Dennis
  • 1,643
  • 9
  • 13
  • I didnot get what you meant by "Remove white between methods argument and the ['GET'] array"?? I tried replaceing spaces with %20...didnt work :( any other suggestions? – Ashraf Choudhury Oct 25 '17 at 19:37
1

I partially solved the problem.

category = str(request.args.get('category'))

This now allows me to pass arguments containing empty spaces.

localhost:5000/v2/products/category?page=1&category=Large Appliances

The above call works fine :

category : Large Appliances

But the problem now arises when my argument contains an ampersand symbol. It is taken as a variable value argument by Flask :

localhost:5000/v2/products/category?page=1&category=Home & Kitchen

Here category gets truncated as :

category : Home

How do i by pass the ampersand symbol so that it is also accepted in teh URL?