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

[BUG]在curl_options中修改HTTPHEADER 报类型错误 #363

Open
mrzcpoGit opened this issue Aug 6, 2024 · 3 comments
Open

[BUG]在curl_options中修改HTTPHEADER 报类型错误 #363

mrzcpoGit opened this issue Aug 6, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@mrzcpoGit
Copy link

mrzcpoGit commented Aug 6, 2024

#方法1
response = requests.post(args.url,headers=headers,impersonate=args.finger,data=bodyString,proxy=args.proxy,timeout=args.timeout,allow_redirects=args.allow_redirects,verify=False,default_headers=False,
            curl_options={
                CurlOpt.HTTPHEADER:tuple(["Content-Type: application/json; charset=UTF-8","Host: xxx.com"])
            })

#方法2
response = requests.post(args.url,headers=headers,impersonate=args.finger,data=bodyString,proxy=args.proxy,timeout=args.timeout,allow_redirects=args.allow_redirects,verify=False,default_headers=False,
            curl_options={
                CurlOpt.HTTPHEADER:["Content-Type: application/json; charset=UTF-8","Host: xxx.com"]
            })

requests.post 自带的设置headers的功能会限制 Content-Type为application/json或者application/x-www-form-urlencoded,由于服务器端只接受application/json; charset=UTF-8,我想通过curl_options设置header,但是我尝试了以上两种方式之后均报同样的错误

Traceback (most recent call last):

  File "tls_req.py", line 12, in <module>

  File "<string>", line 104, in <module>

  File "<string>", line 90, in main

  File "curl_cffi\requests\__init__.py", line 125, in request

  File "curl_cffi\requests\session.py", line 900, in request

  File "curl_cffi\requests\session.py", line 618, in _set_curl_options

  File "curl_cffi\curl.py", line 202, in setopt

TypeError: initializer for ctype 'char *' must be a bytes or list or tuple, not str

[15712] Failed to execute script 'tls_req' due to unhandled exception!
@mrzcpoGit mrzcpoGit added the bug Something isn't working label Aug 6, 2024
@perklet
Copy link
Collaborator

perklet commented Aug 6, 2024

报错里不是已经指了明路了,你得用 bytes 啊。不过为啥不用 headers 参数呢?

@mrzcpoGit
Copy link
Author

谢谢!我不太懂py,见笑了

不用 headers 是因设置Content-Type之后 会被覆盖为 application/json 或者 application/x-www-form-urlencoded
可能是因为session.py中410行处 先设置了headers 之后修改了Content-Type。可能需要个判断,在设置content-Type之前判断传入的headers中是否包含content-type

1722938096859

@perklet
Copy link
Collaborator

perklet commented Aug 6, 2024

有道理,谢谢你的反馈,有空了改下。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants