Python请求POST返回400而不是200

时间:2018-05-23 12:16:26

标签: python-requests

虽然200获得了400,但我期待import requests import json import lxml.html from lxml.cssselect import CSSSelector from lxml.etree import fromstring SELECTOR = CSSSelector('[type=hidden]') BASE_URL = 'https://www.bonuscard.ch/myos/en/login' LOGIN_URL = BASE_URL+'1.IFormSubmitListener-homePanel-loginPanel-loginForm' # headers copied from chromium (returns 200) headers = { "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding" : "gzip, deflate, br", "Accept-Language" : "en,de;q=0.9", "Cache-Control" : "no-cache", "Connection" : "keep-alive", "Content-Length" : "151", "Content-Type" : "application/x-www-form-urlencoded", "DNT" : "1", "Host" : "www.bonuscard.ch", "Origin" : "https: //www.bonuscard.ch", "Pragma" : "no-cache", "Referer" : "https: //www.bonuscard.ch/myos/en/login", "Upgrade-Insecure-Requests" : "1", "User-Agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" } with requests.Session() as session: response = session.get(BASE_URL) tree = lxml.html.fromstring(response.content) keyOnly_token = [e.get('id') for e in SELECTOR(tree)][0] payload = { keyOnly_token:"", "userName-border:userName-border_body:userName ": "jon@doe.com", "password-border:password-border_body:password ": "123", "login ": "" } response = session.post(LOGIN_URL,headers=headers,data=payload) # Returns 400 print(response)

在我的请求中,有人看到我做错了吗?

代码:

headers

这些变化也没有显示出任何区别:

  • 没有json=payload
  • 的POST
  • 使用data=payload代替{{1}}
  • 进行发布

1 个答案:

答案 0 :(得分:0)

感谢Ivan的指示,我发现这个卷曲请求转换器,这是解决方案https://curl.trillworks.com/#