将请求从urlrequest.Request转移到aiohttp ClientSession

时间:2017-10-18 21:47:06

标签: python-3.x request python-asyncio aiohttp

我确实很简单,我会说一个愚蠢的问题。

我有一个简单的代码和平:

  payload_json = json.dumps({                                                       
      "channel": "#general",                                                        
      "username": "webhookbot",                                                     
      "text": msg,                                                                  
      "icon_emoji": ":ghost:"                                                       
  })
  data = urlencode({"payload": payload_json})                                
  opener = urlrequest.build_opener(urlrequest.HTTPHandler())        
  req = urlrequest.Request(config.SLACK_URL)                        
  response = opener.open(req, data.encode('utf-8')).read() 

因为你看不出任何复杂的东西,但是,当我用aiohttp做的时候:

payload_json = json.dumps({                                                       
      "channel": "#general",                                                        
      "username": "webhookbot",                                                     
      "text": msg,                                                                  
      "icon_emoji": ":ghost:"                                                       
})
data = urlencode({"payload": payload_json})
response = await session.post(                                    
  config.SLACK_URL,                                             
  data=bytes(data, 'ascii'),                                    
  headers={'Content-Encoding': 'application/x-www-form-urlencoded'}
)

我总是得到错误:松弛响应invalid_payload,这意味着我向错误的服务器发送了错误的主体。 我有点失落,我做错了什么?

0 个答案:

没有答案