Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always set 'Content-Type': 'application/octet-stream'? #4532

Closed
yjqiang opened this issue Jan 26, 2020 · 11 comments
Closed

Always set 'Content-Type': 'application/octet-stream'? #4532

yjqiang opened this issue Jan 26, 2020 · 11 comments

Comments

@yjqiang
Copy link

yjqiang commented Jan 26, 2020

I'm not sure about this but I think aiohttp should set Content-Type withapplication/x-www-form-urlencoded?

import asyncio

import aiohttp


async def main():
    async with aiohttp.ClientSession() as ss:
        async with ss.request('POST', 'https://www.bilibili.com', params="abc=322") as rsp:
            print(rsp.request_info)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

# RequestInfo(url=URL('https://www.bilibili.com/?abc=322'), method='POST', headers=<CIMultiDictProxy('Host': 'www.bilibili.com', 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'User-Agent': 'Python/3.8 aiohttp/3.6.2', 'Content-Length': '0', 'Content-Type': 'application/octet-stream')>, real_url=URL('https://www.bilibili.com/?abc=322'))
@hh-h
Copy link
Contributor

hh-h commented Jan 26, 2020

There's no body in your request, so there's no way to guess the media type.
see https://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html#sec7.2.1

also, requests doesn't set content-type at all

In [5]: r = requests.post('https://www.bilibili.com', params="abc=322")
In [6]: r.request.headers
Out[6]: {'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Content-Length': '0'}

@yjqiang
Copy link
Author

yjqiang commented Jan 26, 2020

@hh-h Yeah, I tried this with requests. Maybe I'm wrong, so do you know when we should set application/x-www-form-urlencoded?

@hh-h
Copy link
Contributor

hh-h commented Jan 26, 2020

What do you mean by "when we should set"? It's client logic, so feel free to set it whenever you want :)

@yjqiang
Copy link
Author

yjqiang commented Jan 26, 2020

What do you mean by "when we should set"? It's client logic, so feel free to set it whenever you want :)

I mean following the guide like RFC, never mind, I found data and params in aiohttp. And I got this, https://lukasa.co.uk/2013/07/Requests_The_Difference_Between_Params_And_Data/

@yjqiang
Copy link
Author

yjqiang commented Jan 26, 2020

@hh-h Last question, is there any way to print the raw HTTP request? Like requests? https://stackoverflow.com/questions/20658572/python-requests-print-entire-http-request-raw

@hh-h
Copy link
Contributor

hh-h commented Jan 26, 2020

If you want to use library I suggest to dig into docs, it is really helpful :)
https://aiohttp.readthedocs.io/en/stable/client.html

@yjqiang
Copy link
Author

yjqiang commented Jan 26, 2020

If you want to use library I suggest to dig into docs, it is really helpful :)
https://aiohttp.readthedocs.io/en/stable/client.html

I mean I checked this before, and I didn’t find the answer. So …

@hh-h
Copy link
Contributor

hh-h commented Jan 29, 2020

What do you want to achieve?
RequestInfo has all the information about the request, except the body I think. Maybe this will help https://aiohttp.readthedocs.io/en/stable/client_advanced.html#client-tracing too.

@yjqiang
Copy link
Author

yjqiang commented Jan 29, 2020

What do you want to achieve?

“To see what ultimately is constructed from HTTP request?”

@hh-h
Copy link
Contributor

hh-h commented Jan 29, 2020

I'm afraid there's no easy way to view the body of the request (like on SO) without overwriting connector or/and request_class. Latter haven't mentioned in docs tho, see https://aiohttp.readthedocs.io/en/stable/client_reference.html#aiohttp.ClientSession

@yjqiang
Copy link
Author

yjqiang commented Jan 29, 2020

I'm afraid there's no easy way to view the body of the request (like on SO) without overwriting connector or/and request_class. Latter haven't mentioned in docs tho, see https://aiohttp.readthedocs.io/en/stable/client_reference.html#aiohttp.ClientSession

Yeah. And I also find this #2516 . So I think I can close this issue now.
Thanks a lot for your patience. Thank you.

@yjqiang yjqiang closed this as completed Jan 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants