1

I am trying to run the Flask API and want to request data using the API url.
This is my code:

from flask import Flask, jsonify, make_response, request    
app = Flask(__name__)
@app.route('/api/v1.0/qanda/', methods=['GET'])
def people_api():
    text = request.args.get('text')
    if text is None:
       make_response(jsonify({'error': 'Missing text parameter'}), 400)
    return text
app.run()

I have started the application and tried to hit the URL as:

http://127.0.0.1:5000/api/v1.0/qanda/?text=Inshorts invites applications for its Inshorts Inclusives Campus Program Inshorts is looking for young enthusiastic and innovative minds for its campus program – The Inshorts Inclusives Program. The Inshorts Inclusives is a community of like-minded students working towards Inshorts’ umbrella mission of #Stay Informed. The Inclusives being the torchbearers of the mission, are responsible for designing and executing plans and strategies to keep people informed of and connected to, their surroundings and the world at a larger level. Through this journey, an Inclusive gets exposed to the fields of marketing, content-writing, business development and strategy and gets a hands on experience in these areas. WHAT WOULD BE THE WORK OF AN INSHORTS INCLUSIVE? The main task of an Inclusive would be to come-up with innovative solutions to the given problem - of keeping people informed and creating awareness of the Inshorts app amongst the masses. With this problem statement in mind, an Inclusive would need to be on a constant look out for all possible modes and ways of tackling it. An Inclusive would be responsible for both the ideation and execution of such solutions. Along with this, the Inclusives will also drive the initiative of connecting campuses across the country by creating and managing a common content platform for college students on the Inshorts app. For this they will need to ensure and manage their college’s presence on the app by collating all relevant news and information from their college.

The out I received is about 50% of the input I gave:

"Inshorts invites applications for its Inshorts Inclusives Campus Program Inshorts is looking for young enthusiastic and innovative minds for its campus program \u2013 The Inshorts Inclusives Program. The Inshorts Inclusives is a community of like-minded students working towards Inshorts\u2019 umbrella mission of "

I know that the GET method has character limitation. I want to avoid this limitation and get the complete text using the API. Some have suggested using POST method but didn't work as POST needs some form element for fetching the data.

I do not know what to do to get the API accept as many characters as I want.
Kindly, suggest me what I can do in this situation.

VC.One
  • 14,790
  • 4
  • 25
  • 57
Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139

2 Answers2

1

There is very little difference you need to make to your api to get POST to work:

from flask import Flask, jsonify, make_response, request    
app = Flask(__name__)
@app.route('/api/v1.0/qanda/', methods=['POST'])
def people_api():
    text = request.json.get('text')
    if text is None:
       make_response(jsonify({'error': 'Missing text parameter'}), 400)
    return text
app.run()

text = "Inshorts invites applications for its Inshorts Inclusives Campus Program Inshorts is looking for young enthusiastic and innovative minds for its campus program – The Inshorts Inclusives Program. The Inshorts Inclusives is a community of like-minded students working towards Inshorts’ umbrella mission of #Stay Informed. The Inclusives being the torchbearers of the mission, are responsible for designing and executing plans and strategies to keep people informed of and connected to, their surroundings and the world at a larger level. Through this journey, an Inclusive gets exposed to the fields of marketing, content-writing, business development and strategy and gets a hands on experience in these areas. WHAT WOULD BE THE WORK OF AN INSHORTS INCLUSIVE? The main task of an Inclusive would be to come-up with innovative solutions to the given problem - of keeping people informed and creating awareness of the Inshorts app amongst the masses. With this problem statement in mind, an Inclusive would need to be on a constant look out for all possible modes and ways of tackling it. An Inclusive would be responsible for both the ideation and execution of such solutions. Along with this, the Inclusives will also drive the initiative of connecting campuses across the country by creating and managing a common content platform for college students on the Inshorts app. For this they will need to ensure and manage their college’s presence on the app by collating all relevant news and information from their college."

import requests
r = requests.post("http://127.0.0.1:5000/api/v1.0/qanda/", json={"text": text})
r.text
SuperShoot
  • 9,880
  • 2
  • 38
  • 55
  • You may be right. But I get error after running the application. See the image: https://ibb.co/eXihLF – Jaffer Wilson Jul 18 '17 at 02:24
  • In that image, you are still making a GET request but this is set up for POST. https://www.w3schools.com/tags/ref_httpmethods.asp – SuperShoot Jul 18 '17 at 03:10
  • I tried your code buddy. I have used the complete code what you have specified as your answer for my question. – Jaffer Wilson Jul 18 '17 at 03:12
  • I am willing to accept the input from the browser address bar in a single line with the API. Your solution is good but not the one I am looking for. What I am getting the answer is on the commandline, I want to get the output on the browser. Please check this – Jaffer Wilson Jul 18 '17 at 03:19
1

This sounds like a limitation imposed by the browser (255 characters) which is answered in detail here maximum length of HTTP GET request?

Flask specifically lets you change Config settings to control request length using .MAX_CONTENT_LENGTH (If set to a value in bytes, Flask will reject incoming requests with a content length greater than this by returning a 413 status code.) Flask config documentation

Sam Chats
  • 2,271
  • 1
  • 12
  • 34
Giantrun
  • 21
  • 6