APIリクエストがJSONオブジェクトではなくtext/htmlを返します

@sam素晴らしいガイドによると、目的のリクエストに適切なエンドポイントを見つけました。

https://domain.com/admin/reports/bulk?reports[dau_by_mau][facets][]=prev_period&reports[dau_by_mau][start_date]=2022-07-27&reports[dau_by_mau][end_date]=2022-08-28&reports[dau_by_mau][limit]=50

フォームヘッダーは存在せず、必要ないだろうと推測しました。Dev Tools でこのリクエストを行うと、まさに求めている JSON レスポンスが返されます。Python で GET リクエストを作成しようとしています。

url = "https://domain.com/admin/reports/bulk?reports[dau_by_mau][facets][]=prev_period&reports[dau_by_mau][start_date]=2022-07-27&reports[dau_by_mau][end_date]=2022-08-28&reports[dau_by_mau][limit]=50.json"
headers = CaseInsensitiveDict()
headers["Content-Type"] = "multipart/form-data"
headers["Content-Type"] = "application/json"
headers["Api-Key"] = SECRET.API
headers["Api-Username"] = "system"

resp = requests.get(url, headers=headers)

print(resp.status_code)
print(resp.headers)

200 は返されますが、JSON ではなく 'Content-Type': 'text/html' で返されます(求めているのは JSON です)。また、返されたテキストが目的のデータのテキストバージョンであるかどうかも定かではありません。

これも役に立ちませんでした。

では、何が間違っているのでしょうか?

「いいね!」 1

重複したヘッダーがあります。

Content-Type は送信するものに関するもので、Accept は返してほしいものに関するものです。
したがって、これを次のように変更する必要があります。

headers["Content-Type"] = "multipart/form-data"
headers["Accept"] = "application/json"
「いいね!」 5

すみません、これは無関係な遺物です。2番目のヘッダーがないと問題が発生しました。

これは機能します。ありがとうございます!

「いいね!」 2

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.