为什么我的Python发布请求返回400错误?

时间:2019-11-04 08:52:25

标签: post python-requests

我正在尝试使用政府土地定位器应用获取已知土地的经纬度坐标。该网站为www.makani.ae

说我在搜索字段中输入土地编号“ 3520461”并进行搜索。我正在尝试像这样复制帖子请求:

import requests

url = 'https://www.makani.ae/makaniproxy/Makani.svc/getmakanidatanew'

headers = {
            "Host": "www.makani.ae",
            "Connection": "keep-alive",
            "Content-Length": "291",
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "Origin": "http://www.makani.ae",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36",
            "Content-Type": "application/json;charset=UTF-8",
            "Sec-Fetch-Site": "cross-site",
            "Sec-Fetch-Mode": "cors",
            "Referer": "http://www.makani.ae/desktop/index.html?25",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "en-US,en;q=0.9"
            }
payload = {
            'parameters': {
                            'InputJson': {
                                            'searchtext': '3520461',
                                            'lang': 'E',
                                            'currentlocation': '25.26452971,55.31196410',
                                            'distancetype': 'KM',
                                            'level': '2',
                                            'userid': '',
                                            'sessionid': ''},
                            'Token': 'b7s5isip5p6nkrenhfbd9bdbg8!=+=BDEE6d4K4CL6nplm720bIA==',
                            'Remarks': 'Makani Phase 2'},
            'url': 'http://www.makani.ae/MakaniPhase2ProxyWebService/MakaniPhase2Proxy.svc/SmartSearch'
            }

r = requests.post(url,data=payload, headers=headers)

这将返回400错误。我要去哪里错了?

2 个答案:

答案 0 :(得分:0)

您的标题看起来有点不对劲。我建议用这个替换当前的标题:

headers = {
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "Origin": "http://www.makani.ae",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36",
            "Content-Type": "application/json;charset=UTF-8",
            "Sec-Fetch-Site": "cross-site",
            "Sec-Fetch-Mode": "cors",
            "Referer": "http://www.makani.ae/desktop/index.html?25",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "en-US,en;q=0.9"
            }

希望这会有所帮助

答案 1 :(得分:0)

我意识到问题出在我的有效负载变量中的嵌套字典中。归功于https://stackoverflow.com/a/57999804。下面的工作代码:

import requests
import json

url = 'https://www.makani.ae/makaniproxy/Makani.svc/getmakanidatanew'

headers = {
            "Host": "www.makani.ae",
            "Connection": "keep-alive",
            "Content-Length": "291",
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "Origin": "http://www.makani.ae",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36",
            "Content-Type": "application/json;charset=UTF-8",
            "Sec-Fetch-Site": "cross-site",
            "Sec-Fetch-Mode": "cors",
            "Referer": "http://www.makani.ae/desktop/index.html?25",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "en-US,en;q=0.9"
            }
payload = {
            'parameters': {
                            'InputJson': {
                                            'searchtext': '3520461',
                                            'lang': 'E',
                                            'currentlocation': '25.26452971,55.31196410',
                                            'distancetype': 'KM',
                                            'level': '2',
                                            'userid': '',
                                            'sessionid': ''},
                            'Token': 'b7s5isip5p6nkrenhfbd9bdbg8!=+=BDEE6d4K4CL6nplm720bIA==',
                            'Remarks': 'Makani Phase 2'},
            'url': 'http://www.makani.ae/MakaniPhase2ProxyWebService/MakaniPhase2Proxy.svc/SmartSearch'
            }

r = requests.post(url,data=json.dumps(payload), headers=headers)
相关问题