2017-07-17 5 views
0

Ich versuche, die Flask-API auszuführen und Daten mithilfe der API-URL anfordern möchten.
Dies ist mein Code:GET-Methode und der größere Text mit Kolben und 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() 

ich die Anwendung gestartet haben und versucht, die URL zu treffen wie:

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. 

Die I etwa 50% des Eingangs erhielt ich gab:

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

Ich weiß, dass die GET Methode Zeichenbegrenzung hat. Ich möchte diese Einschränkung vermeiden und den vollständigen Text mithilfe der API abrufen. Einige haben vorgeschlagen, die POST-Methode zu verwenden, aber sie haben nicht funktioniert, da POST ein Formularelement zum Abrufen der Daten benötigt.

Ich weiß nicht, was zu tun ist, um die API zu erhalten so viele Zeichen wie ich möchte.
Bitte, schlagen Sie mir vor, was ich in dieser Situation tun kann.

+0

Ich denke POST ist Ihre Antwort, – SuperShoot

Antwort

1

Es gibt sehr wenig Unterschied, den Sie zu Ihrem api machen müssen, um POST an die Arbeit:

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

Sie können Recht haben. Aber ich bekomme einen Fehler nach dem Ausführen der Anwendung. Siehe das Bild: https://ibb.co/eXihLF –

+0

In diesem Bild machen Sie immer noch eine GET-Anfrage, aber diese ist für POST eingerichtet. https://www.w3schools.com/tags/ref_httpmethods.asp – SuperShoot

+0

Ich habe versucht, Ihren Code Buddy. Ich habe den vollständigen Code verwendet, den Sie als Antwort auf meine Frage angegeben haben. –

1

Das klingt wie eine vom Browser auferlegten Beschränkung (255 Zeichen), die hier im Detail beantwortet wird maximum length of HTTP GET request?

Flask speziell können Sie Config Einstellungen ändern Anforderungslänge .MAX_CONTENT_LENGTH mit steuern (Wenn in Bytes auf einen Wert gesetzt, Flask eingehende Anfragen mit einem Gehalt Länge ablehnen größer ist als dies durch einen 413-Statuscode zurück.) Flask config documentation

+0

Sorry, aber es hat nicht in meinem Fall funktioniert. Können Sie mir sagen, wie Sie das GET-Methodenlimit erhöhen können? –