Python requests 请求导致 400 错误,而 cURL 请求正常
Python requests request leads to 400 error while cURL request is OK
我使用请求如下:
headers = {
'Authorization': f'Bearer {self.__yandex_cloud_manager.get_iam_token().token}',
'Transfer-Encoding': 'chunked',
}
data = {
'voice': self.voice,
'emotion': 'neutral',
'lang': self.language,
'speed': self.temp,
'folderId': self.__get_folder_id(),
'format': 'oggopus',
'text': text,
}
response = requests.post(
url='https://tts.api.cloud.yandex.net/speech/v1/tts:synthesize',
headers=headers,
data=data)
它发送这个数据:
send: b'POST /speech/v1/tts:synthesize HTTP/1.1\r\nHost: tts.api.cloud.yandex.net\r\nUser-Agent: python-requests/2.21.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nAuthorization: Bearer <removed>\r\nTransfer-Encoding: chunked\r\nContent-Length: 135\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n'
send: b'voice=alyss&emotion=neutral&lang=ru-RU&speed=1.0&folderId=<removed>&format=oggopus&text=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82'
我收到了 400 个响应。
但是当我像这样转储请求时:
req = response.request
command = "curl -X {method} -H {headers} -d '{data}' '{uri}'"
method = req.method
uri = req.url
data = req.body
headers = ['"{0}: {1}"'.format(k, v) for k, v in req.headers.items()]
headers = " -H ".join(headers)
print(command.format(method=method, headers=headers, data=data, uri=uri))
它给出了以下命令:
curl -X POST -H "User-Agent: python-requests/2.21.0" -H "Accept-Encoding: gzip, deflate" -H "Accept: */*" -H "Connection: keep-alive" -H "Authorization: Bearer <removed>" -H "Transfer-Encoding: chunked" -H "Content-Length: 135" -H "Content-Type: application/x-www-form-urlencoded" -d 'voice=alyss&emotion=neutral&lang=ru-RU&speed=1.0&folderId=<removed>&format=oggopus&text=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82' 'https://tts.api.cloud.yandex.net/speech/v1/tts:synthesize'
并且使用此命令的请求执行成功。
可能是什么问题?
好吧,如果我删除 "Transfer-Encoding: chunked"
header。
我使用请求如下:
headers = {
'Authorization': f'Bearer {self.__yandex_cloud_manager.get_iam_token().token}',
'Transfer-Encoding': 'chunked',
}
data = {
'voice': self.voice,
'emotion': 'neutral',
'lang': self.language,
'speed': self.temp,
'folderId': self.__get_folder_id(),
'format': 'oggopus',
'text': text,
}
response = requests.post(
url='https://tts.api.cloud.yandex.net/speech/v1/tts:synthesize',
headers=headers,
data=data)
它发送这个数据:
send: b'POST /speech/v1/tts:synthesize HTTP/1.1\r\nHost: tts.api.cloud.yandex.net\r\nUser-Agent: python-requests/2.21.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nAuthorization: Bearer <removed>\r\nTransfer-Encoding: chunked\r\nContent-Length: 135\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n'
send: b'voice=alyss&emotion=neutral&lang=ru-RU&speed=1.0&folderId=<removed>&format=oggopus&text=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82'
我收到了 400 个响应。
但是当我像这样转储请求时:
req = response.request
command = "curl -X {method} -H {headers} -d '{data}' '{uri}'"
method = req.method
uri = req.url
data = req.body
headers = ['"{0}: {1}"'.format(k, v) for k, v in req.headers.items()]
headers = " -H ".join(headers)
print(command.format(method=method, headers=headers, data=data, uri=uri))
它给出了以下命令:
curl -X POST -H "User-Agent: python-requests/2.21.0" -H "Accept-Encoding: gzip, deflate" -H "Accept: */*" -H "Connection: keep-alive" -H "Authorization: Bearer <removed>" -H "Transfer-Encoding: chunked" -H "Content-Length: 135" -H "Content-Type: application/x-www-form-urlencoded" -d 'voice=alyss&emotion=neutral&lang=ru-RU&speed=1.0&folderId=<removed>&format=oggopus&text=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82' 'https://tts.api.cloud.yandex.net/speech/v1/tts:synthesize'
并且使用此命令的请求执行成功。
可能是什么问题?
好吧,如果我删除 "Transfer-Encoding: chunked"
header。