第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

無法從燒瓶 api 獲得評(píng)論

無法從燒瓶 api 獲得評(píng)論

SMILET 2022-06-28 10:57:56
嘗試獲取添加評(píng)論的文本時(shí),我在 flask.request.form 中使用以下 curl 命令獲取關(guān)鍵錯(cuò)誤。我嘗試打印出 flask.request.form 但它是空的。如何解決?curl 命令添加新評(píng)論:curl -ib cookies.txt   --header 'Content-Type: application/json'   --request POST   --data '{"text":"Comment sent from curl"}'  http://localhost:8000/api/v1/p/3/comments/錯(cuò)誤:werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.KeyError: 'text'我的評(píng)論對(duì)象:  <form id="comment-form">  <input type="text" value=""/>  </form>我的 flask.api python 文件將返回新的評(píng)論字典:@app.route('/api/v1/p/<int:postid>/comments/', methods=["POST"])def add_comment(postid):     db = model.get_db()    owner = flask.session["username"]    query = "INSERT INTO comments(commentid, owner, postid, text, created) VALUES(NULL, ?, ?, ?, DATETIME('now'))"    db.execute(query, (owner, postid, flask.request.form["text"]))    last_id = db.execute("SELECT last_insert_rowid()").fetchone()["last_insert_rowid()"]    get_newest_comment_query = "SELECT * FROM comments WHERE commentid = ?"    comment = db.execute(get_newest_comment_query, (last_id,)).fetchone()    print('get comment: ', comment)    return flask.jsonify(comment), 201
查看完整描述

2 回答

?
慕少森

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊

添加到@Harshal 的答案中,使用 curl 時(shí),您似乎在錯(cuò)誤地訪問請(qǐng)求數(shù)據(jù)。由于請(qǐng)求的 Content-Type 設(shè)置為application/json,因此您需要使用flask.request.json-更多詳細(xì)信息來訪問請(qǐng)求數(shù)據(jù)

或者您可以curl像下面這樣更新命令,

curl -ib cookies.txt   
  --request POST   
  --data-urlencode "text=Comment sent from curl"  
  http://localhost:8000/api/v1/p/3/comments/

在這種情況下,curl 將自動(dòng)使用Content-Type application/x-www-form-urlencoded,您的應(yīng)用程序?qū)⒛軌蚴褂?code>flask.request.form


查看完整回答
反對(duì) 回復(fù) 2022-06-28
?
翻翻過去那場(chǎng)雪

TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊

您的 HTML 表單配置不正確。

  1. 您正在發(fā)送一個(gè) GET 請(qǐng)求,而您的燒瓶只接受 POST。

  2. flask.request.form["text"]要求輸入名為 text 的輸入,但您的文本框沒有任何名稱。

  3. 沒有提交按鈕。

您可以通過以下方式修復(fù)它:

<form id="comment-form" method="post">

    <input type="text" value="" name="text" />

    <input type="submit" />

</form>

如果您了解更多有關(guān)響應(yīng)代碼的信息,您可能會(huì)更容易調(diào)試:https ://developer.mozilla.org/en-US/docs/Web/HTTP/Status 。


由于您正在使用request.form,因此可以像這樣簡(jiǎn)化 tour curl:


curl --form "text=some_text_here" http://localhost:8000/api/v1/p/3/comments/

希望這可以幫助。祝你好運(yùn)。


查看完整回答
反對(duì) 回復(fù) 2022-06-28
  • 2 回答
  • 0 關(guān)注
  • 209 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)