Transfer-Encoding: chunked

调用网页中的API怎么将参数中的中文转为这种格式呢?调用网页中的API怎么将参数中的中文转为这种格式呢?

img

img

        fields = {
            'BeginDate': (None, begin_date),
            'EndDate': (None, end_date),
            'PrintState': (None, '0'),
            'AcNo': (None, ''),
            'CifName': (None,'投资有限公司'),
            'CurrencyName': (None, '人民币'),
            'AccountType': (None, '0'),
            'PdfStyle': (None, '0'),
            'ReverseFlag': (None, '0'),
            'dataType': (None, 'callback'),
        }
            boundary = '---------------------------7e63' + ''.join(
            random.sample(string.ascii_letters.lower() + string.digits, 9))
            head1['Content-Type'] = 'multipart/form-data; boundary=' + boundary
            fields['AcNo'] = (None, num)
            m = MultipartEncoder(fields=fields, boundary=boundary)

【以下回答由 GPT 生成】

在处理API参数时,将中文字符转换为Transfer-Encoding: chunked格式的方法通常是使用URL编码。URL编码将特殊字符编码为%xx的格式,其中xx代表特殊字符的16进制ASCII码。

以下是在给定代码中如何将参数中的中文字符转换为Transfer-Encoding: chunked格式的步骤:

  1. 引入urllib.parse库
import urllib.parse
  1. 在构建fields字典时,将包含中文字符的参数值使用urllib.parse.quote()函数进行URL编码。
fields = {
    'BeginDate': (None, begin_date),
    'EndDate': (None, end_date),
    'PrintState': (None, '0'),
    'AcNo': (None, ''),
    'CifName': (None, urllib.parse.quote('投资有限公司')),
    'CurrencyName': (None, urllib.parse.quote('人民币')),
    'AccountType': (None, '0'),
    'PdfStyle': (None, '0'),
    'ReverseFlag': (None, '0'),
    'dataType': (None, 'callback'),
}
  1. 保留原来的boundary生成代码不变。
boundary = '---------------------------7e63' + ''.join(
    random.sample(string.ascii_letters.lower() + string.digits, 9))
head1['Content-Type'] = 'multipart/form-data; boundary=' + boundary
fields['AcNo'] = (None, num)
m = MultipartEncoder(fields=fields, boundary=boundary)

通过上述步骤,你可以将参数中的中文字符正确地转换为Transfer-Encoding: chunked格式。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^