psf / requests

A simple, yet elegant, HTTP library.
https://requests.readthedocs.io/en/latest/
Apache License 2.0
52.16k stars 9.33k forks source link

TypeError: Object of type bytes is not JSON serializable #6462

Closed wanghaisheng closed 1 year ago

wanghaisheng commented 1 year ago

Expected Result

am i wrong about using this

Actual Result

Traceback (most recent call last):
  File "/Users/wenke/github/ttsall/tts-test.py", line 36, in speak
    response = requests.post('https://translate.volcengine.com/crx/tts/v1/',  headers=headers, json= json.dumps(json_data, ensure_ascii=False).encode("utf-8"),proxies={'http':None,'https':None})
  File "/Users/wenke/miniconda3/lib/python3.9/site-packages/requests/api.py", line 115, in post
    return request("post", url, data=data, json=json, **kwargs)
  File "/Users/wenke/miniconda3/lib/python3.9/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "/Users/wenke/miniconda3/lib/python3.9/site-packages/requests/sessions.py", line 573, in request
    prep = self.prepare_request(req)
  File "/Users/wenke/miniconda3/lib/python3.9/site-packages/requests/sessions.py", line 484, in prepare_request
    p.prepare(
  File "/Users/wenke/miniconda3/lib/python3.9/site-packages/requests/models.py", line 371, in prepare
    self.prepare_body(data, files, json)
  File "/Users/wenke/miniconda3/lib/python3.9/site-packages/requests/models.py", line 511, in prepare_body
    body = complexjson.dumps(json, allow_nan=False)
  File "/Users/wenke/miniconda3/lib/python3.9/json/__init__.py", line 234, in dumps
    return cls(
  File "/Users/wenke/miniconda3/lib/python3.9/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Users/wenke/miniconda3/lib/python3.9/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/Users/wenke/miniconda3/lib/python3.9/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable

Reproduction Steps

        headers = {
            'authority': 'translate.volcengine.com',
            'accept': 'application/json, text/plain, */*',
            'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
            'origin': 'chrome-extension://jmnhemdajboodicneejdlpanmijclhef',
            'accept-encoding': 'gzip, deflate, br',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'none',
            # 'cookie':'x-jupiter-uuid=1685206167481291; i18next=zh-CN; s_v_web_id=verify_li6888fu_E2yIFhRA_Amo7_42lt_93A0_OrjFArSkTope; ttcid=29381a8ff7874daea9f91f7bb6a91c4b41; isIntranet=-1; ve_doc_history=4640; tt_scid=QmoChhEFFYvYA9lNkQwiGu8VOuOiJDyGbcNg7Ysc9h4O-ceoZRVLq0cw5H4qbVSl79af; __tea_cache_tokens_3569={"web_id":"7237905415249118780","user_unique_id":"7237905415249118780","timestamp":1685208803602,"_type_":"default"}; referrer_title=音视频文件翻译API--机器翻译-火山引擎',
            'content-type':'application/json; charset=UTF-8',
            'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.51',

        }

        json_data = {
            "text": content,
            "speaker":voice,
            "language":"zh",

        }
        json_data={"text":"我们自豪地向您介绍火山写作:一款中英双语的AI写作助手。您可以使用它来撰写和润色您的论文、博客、电子邮件等!在写作英文时,火山写作会实时扫描您的文章,提供修改和增强建议,使您的英文表达更流畅、准确、恰当。如下所示:","speaker":"zh_female_sichuan","language":"zh"}

        response = requests.post('https://translate.volcengine.com/crx/tts/v1/',  headers=headers, json= json.dumps(json_data, ensure_ascii=False).encode("utf-8"),proxies={'http':None,'https':None})
        print(response,response.status_code,response.content)

System Information

$ python -m requests.help
/Users/wenke/miniconda3/lib/python3.9/site-packages/requests/help.py:24: DeprecationWarning: 'urllib3.contrib.pyopenssl' module is deprecated and will be removed in a future release of urllib3 2.x. Read more in this issue: https://github.com/urllib3/urllib3/issues/2680
  from urllib3.contrib import pyopenssl
{
  "chardet": {
    "version": "5.1.0"
  },
  "charset_normalizer": {
    "version": "2.0.4"
  },
  "cryptography": {
    "version": "39.0.1"
  },
  "idna": {
    "version": "3.4"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.9.16"
  },
  "platform": {
    "release": "19.6.0",
    "system": "Darwin"
  },
  "pyOpenSSL": {
    "openssl_version": "1010114f",
    "version": "23.0.0"
  },
  "requests": {
    "version": "2.28.1"
  },
  "system_ssl": {
    "version": "1010114f"
  },
  "urllib3": {
    "version": "1.26.15"
  },
  "using_charset_normalizer": false,
  "using_pyopenssl": true
}
nateprewitt commented 1 year ago

json serializes Python dictionaries to JSON. data takes arbitrary bytes.

https://docs.python-requests.org/en/latest/api/#requests.request

wanghaisheng commented 1 year ago

@nateprewitt can you help me about

{'text': '我们自豪地向您介绍火山写作:一款中英双语的AI写作助手。您可以使用它来撰写和润色您的论文、博客、电子邮件等!在写作英文时,火山写作会实时扫描您的文章供修改和增强建议,使您的英文表达更流畅、准确、恰当。如下所示:', 'speaker': 'BV406_streaming', 'language': 'zh'}

why after I set the param , print the json data , double quotation mark become single quotation marks

nateprewitt commented 1 year ago

@wanghaisheng You're printing a Python dictionary, not the raw JSON string.

Please direct any other requests for assistance to the python-requests tag on https://stackoverflow.com. We won't be able to assist on this issue tracker. Thanks!