Strawpoll.me API returns 我的 Python 程序的“400 错误请求”,但可以在终端上运行

Strawpoll.me API returns "400 Bad Request" for my Python program, but works from terminal

我有以下 Python 程序可以使用 strawpoll.me 的 API 创建草民投票,如 here.

所述
import aiohttp
import asyncio

async def createPoll():
    question = "Is this thing on?"
    options = ["Yes", "No", "Maybe"]

    try:
        async with aiohttp.ClientSession() as session:
            async with session.post(
                "https://www.strawpoll.me/api/v2/polls",
                json={
                    "title": question,
                    "options": options,
                    "multi": "false"
                },
                headers={"Content Type": "application/json"}
            ) as response:
                print(response)
                json = await response.json()
                strawpoll_id = json["id"]
            print(f"https://strawpoll.me/{strawpoll_id}")
    except Exception as e:
        print(e)

if __name__ == '__main__':
    asyncio.run(createPoll())

这曾经有用,但现在不行了。我想知道发生了什么变化。这是我现在得到的:

<ClientResponse(https://www.strawpoll.me/api/v2/polls) [400 Bad Request]>
<CIMultiDictProxy('Connection': 'close', 'Content-Length': '0')>

0, message='Attempt to decode JSON with unexpected mimetype: '
>>> 

如果我在这样的终端中将相同的参数发送到端点:

curl -H "Content-Type: application/json" -X POST -d '{"title": "Is this this on?", "options": ["Yes", "No", "Maybe"], "multi": "false"}' https://www.strawpoll.me/api/v2/polls

我得到了预期的响应。

我试过了:


async def createPoll():
    question = "Is this thing on?"
    options = ["Yes", "No", "Maybe"]

    try:
        async with aiohttp.ClientSession() as session:
            print('HERE')
            async with session.post(
                "https://www.strawpoll.me/api/v2/polls",
                json={
                    "title": question,
                    "options": options,
                    "multi": "false"
                },
                headers={"Content-Type": "application/json"}
            ) as response:
                print(response)
                json = await response.json()
                strawpoll_id = json["id"]
            print(f"https://strawpoll.me/{strawpoll_id}")
    except Exception as e:
        print(e)

我唯一改变的是 Content TypeContent-Type 和我得到的输出:

<ClientResponse(https://www.strawpoll.me/api/v2/polls) [200 OK]>
<CIMultiDictProxy('Access-Control-Allow-Headers': 'X-Requested-With', 'Access-Control-Allow-Origin': '*', 'Cache-Control': 'private, no-cache="set-cookie"', 'Content-Type': 'application/json; charset=utf-8', 'Server': 'Microsoft-IIS/10.0', 'Set-Cookie': 'AWSELB=193D972F16169235196B775BF1B5BFF2D266A3E7F134840A661FD488EC125877D4193B20C4FCDBDB753E1C0E576E03398F3B7AAE5AAC9D70BFE31DB4F45EABEEB6F46BFBEE;PATH=/;MAX-AGE=3600', 'Set-Cookie': 'AWSELBCORS=193D972F16169235196B775BF1B5BFF2D266A3E7F134840A661FD488EC125877D4193B20C4FCDBDB753E1C0E576E03398F3B7AAE5AAC9D70BFE31DB4F45EABEEB6F46BFBEE;PATH=/;MAX-AGE=3600;SECURE;SAMESITE=None', 'X-AspNet-Version': '4.0.30319', 'X-AspNetMvc-Version': '5.2', 'X-Frame-Options': 'DENY', 'X-UA-Compatible': 'IE=edge,chrome=1', 'Content-Length': '140', 'Accept-Ranges': 'bytes', 'Date': 'Fri, 06 Mar 2020 07:47:42 GMT', 'Via': '1.1 varnish', 'Connection': 'keep-alive', 'X-Served-By': 'cache-hhn4027-HHN', 'X-Cache': 'MISS', 'X-Cache-Hits': '0', 'X-Timer': 'S1583480862.671417,VS0,VE458', 'Set-Cookie': 'Geo={%22region%22:%22HE%22%2C%22country%22:%22DE%22%2C%22continent%22:%22EU%22}; path=/; domain=.strawpoll.me; SameSite=None; Secure;', 'Strict-Transport-Security': 'max-age=300')>