2017-07-17 2 views
0

J'essaie d'exécuter l'API Flask et je souhaite demander des données en utilisant l'URL de l'API.
Ceci est mon code:Méthode GET et texte plus grand utilisant flask et python3

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() 

J'ai commencé l'application et a essayé de frapper l'URL comme:

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. 

SORTIR J'ai reçu environ 50% de l'entrée que j'ai donné:

"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 " 

Je sais que la méthode GET a une limitation de caractères. Je veux éviter cette limitation et obtenir le texte complet en utilisant l'API. Certains ont suggéré d'utiliser la méthode POST mais n'ont pas fonctionné car POST a besoin d'un élément de formulaire pour récupérer les données.

Je ne sais pas quoi faire pour que l'API accepte autant de caractères que je le souhaite.
Veuillez me suggérer ce que je peux faire dans cette situation.

+0

Je pense que le POST est votre réponse, – SuperShoot

Répondre

1

Il y a très peu de différence que vous devez faire à votre api pour obtenir POST au travail:

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 
+0

Vous avez peut-être raison. Mais je reçois une erreur après l'exécution de l'application. Voir l'image: https://ibb.co/eXihLF –

+0

Dans cette image, vous faites toujours une requête GET mais celle-ci est configurée pour POST. https://www.w3schools.com/tags/ref_httpmethods.asp – SuperShoot

+0

J'ai essayé votre copain de code. J'ai utilisé le code complet que vous avez spécifié comme réponse à ma question. –

1

Cela ressemble à une limitation imposée par le navigateur (255 caractères) qui a répondu en détail ici maximum length of HTTP GET request?

Flask vous permet de modifier spécifiquement Config les paramètres pour contrôler la longueur de la demande à l'aide .MAX_CONTENT_LENGTH (Si elle est définie à une valeur en octets, Flask rejettera les requêtes entrantes avec une longueur de teneur supérieure en retournant un code 413 d'état.) Flask config documentation

+0

Désolé mais cela n'a pas fonctionné dans mon cas. Pouvez-vous me dire comment augmenter la limite de la méthode 'GET'? –