用Flask和Python3得到方法和更大的文本

2024-06-24 13:01:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试运行Flask API,并希望使用API url请求数据。
这是我的代码:

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

我已经启动了应用程序,并尝试将URL设置为:

^{pr2}$

我收到的输出大约是我输入的50%:

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

我知道GET方法有字符限制。我想避免这个限制,并使用API获得完整的文本。有些人建议使用POST方法,但由于POST需要一些form元素来获取数据,所以不起作用。在

我不知道该怎么做才能让API接受尽可能多的字符。
请告诉我在这种情况下我能做些什么。在


Tags: textapiappflaskforgetmakeis
2条回答

要使POST生效,您只需对api进行很少的更改:

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

这听起来像是浏览器(255个字符)施加的限制,这里将详细回答maximum length of HTTP GET request?

Flask特别允许您更改Config设置,以使用.MAX_CONTENT_LENGTH控制请求长度(如果设置为字节值,Flask将通过返回413状态代码来拒绝内容长度大于此值的传入请求。) Flask config documentation

相关问题 更多 >