Django コンシューマーに API を提供する Flask アプリがあります。コンシューマーでrequests ライブラリを使用して API にアクセスします。
私の問題は次のとおりです。API をテストすると、POST データが で取得request.form
され、コンシューマーから (リクエスト ライブラリを使用して) ヒットすると、POST データが で取得されrequest.data
ます。
例えば、
Flask アプリの API エンドポイント:
@mod.route('/customers/', methods=['POST'])
def create_prospect():
customer = Customer()
prospect = customer.create_prospect(request.form)
return jsonify(prospect.serialize()), 201
Flask アプリでの API エンドポイントのテスト:
def test_creating_prospect(self):
with self.app.app_context():
data = {'name': 'Test company and co'}
response = self.client.post(self.url, data=data)
...
request.form
これは私のエンドポイントに入力され、正常に動作します。
リクエストを使用して、Django アプリから API を使用します。
...
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
data = {'name': 'Test company and co'}
response = requests.post(url, data=data, headers=headers)
これrequest.data
はエンドポイントに入力されますが、情報を確認していると失敗しますrequest.form
。
この質問を書いている間、私は考えました。たぶん、jsonヘッダーがrequest.data
代わりに作成されていrequest.form
ますか?
任意の入力をいただければ幸いです。
編集 - テストにヘッダーを追加しようとしましたが、うまくいきました:
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
response = self.client.post(self.url, data=data, headers=headers)