我在这个json请求中做错了什么?

时间:2018-03-21 19:57:06

标签: python json request python-requests urllib

我试图从https://www.correoargentino.com.ar/formularios/cpa中的表单中获得回复 看到请求标题我做了以下代码:

import json, requests


import requests
import logging

try:
    import http.client as http_client
except ImportError:
    # Python 2
    import httplib as http_client

http_client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True


url = "https://www.correoargentino.com.ar/sites/all/modules/custom/ca_forms/api/wsFacade.php/"
code = 'H'
params = dict(
    action="localidades",
    localidad="none",
    calle="",
    altura="",
    provincia=code,
)

resp = requests.get(url=url, params=params,headers={
        'Referer': 'https://www.correoargentino.com.ar/formularios/cpa', 
        'Origin': 'https://www.correoargentino.com.ar',
        'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'X-Requested-With':'XMLHttpRequest',    
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'es-419,es;q=0.9,en-US;q=0.8,en;q=0.7',
        'Cookie': '_ga=GA1.3.2079472460.1521655930; _gid=GA1.3.2041549384.1521655930; has_js=1',
})
print resp.text

但是我收到错误,当我看到Chrome的响应时,我得到了正确的回复。我打了它是因为请求的起源/引用,但后来我改变了标题并继续得到同样的错误。

欢迎任何帮助

1 个答案:

答案 0 :(得分:1)

将您使用的方法更改为.post,请记住此方法中的第二个参数为data

resp = requests.post(url=url, data=params)