【问题标题】:How should I convert the following http.client request in Requests library form ? [Python]我应该如何在 Requests 库表单中转换以下 http.client 请求? [Python]
【发布时间】:2017-04-07 21:03:42
【问题描述】:

我想把下面的http.client请求代码转换成Requests 图书馆形式。

我尝试过这样做,但一直在思考 requests.post(url, data=None, json=None, **kwargs) 中的正文和标题都将在哪里传递。 我需要使用 requests 库,主要是因为我想让它异步。

headers = {"Content-type": "application/ssml+xml",
           "X-Microsoft-OutputFormat": "audio-16khz-128kbitrate-mono-mp3",
           "Authorization": "Bearer " + access_token,
           "X-Search-AppId": "__ID__",
           "X-Search-ClientID": "__ID__",
           "User-Agent": "TTSForPython"}
body = "<speak version='1.0' xml:lang='en-us'><voice xml:lang='en-CA' xml:gender='Female' name='Microsoft Server Speech Text to Speech Voice (en-CA, HeatherRUS)'>" + text + "</voice></speak>"
conn = http.client.HTTPSConnection("speech.platform.bing.com")
conn.request("POST", "/synthesize", body, headers)
response = conn.getresponse()

谢谢!!

【问题讨论】:

    标签: python python-2.7 http python-requests http.client


    【解决方案1】:

    您的body,实际上只是有效载荷,因此将作为data 参数传递。

    标头只是作为,嗯,标头传递。

    r = requests.post(url='http:/speech.platform.bing.com/synthesize',
                      data=body,
                      headers=headers)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 2019-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多