首页 > 解决方案 > 将字符串转换为http字符串格式

问题描述

我需要提出和 api 请求,我需要转换一个字符串,如:

"hello i am a special string + i contain special characters?"

转换成如下请求格式:

"hello%20i%20am%20a%20special ... ecc"

python有帮助我的库吗?

这是我的代码:

import http.client
conn = http.client.HTTPSConnection("address:port")
conn.request(method="GET", url="/my/target/site?info1=this needs to be converted&info2=also this???", headers=headers)

谢谢

标签: pythonhttphttp-headersspecial-characters

解决方案


就像如何使用 python 将 url 字符串转换为安全字符? 以及 如何在 Python 中对查询字符串进行 urlencode?

您可以使用

import urllib
urllib.parse.quote_plus('string here')

推荐阅读