How to translate a cURL request to Python's aiohttp?

Viewed 28

I need to add a function to my Python project that checks comments for toxicity. The example cURL is this:

 curl -H "Content-Type: application/json" --data \
    '{comment: {text: "what kind of idiot name is foo?"},
       languages: ["en"],
       requestedAttributes: {TOXICITY:{}} }' \
https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key=YOUR_KEY_HERE

Now there, is also an example Python code. But that is no good, because it's synchronous. I need it to be async, I need to use aiohttp. This is my attempt to translate the cURL request:

import aiohttp, asyncio

async def main():
    async with aiohttp.ClientSession(headers={"CONTENT-TYPE": "application/json"}) as session:
        async with session.get("https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key"
                               "=",
                               json={"comment": {"text": "what kind of idiot name is foo?"},
                                     "languages": ["en"],
                                     "requestedAttributes": {"TOXICITY": {}}},
                               ) as resp:
            print(resp)


asyncio.run(main())

(I've hid my API key) Unfortunately, that doesn't work, that yields:

<ClientResponse(https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key=) [400 Bad Request]>
<CIMultiDictProxy('Content-Type': 'text/html; charset=UTF-8', 'Referrer-Policy': 'no-referrer', 'Content-Length': '1555', 'Date': 'Thu, 22 Sep 2022 09:37:52 GMT')>

How do I fix this? I've went through the aiohttp docs, tried many things, played around with the kwargs and I still get the same thing. Please help

EDIT:

So, after some playing around in Postman, I managed to send a successful request. There were a couple mistakes. First off, it has to be a POST request. Second of all, it didn't work without these 2 headers:

Host: commentanalyzer.googleapis.com
Content-Length: 160

Content-Length is calculated automatically. Problem is when I try to do that in Pycharm on Fedora, it doesn't work. It hangs. After setting a timeout of 3 seconds, it raises that error.

0 Answers
Related