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

为什么我的Python Post请求返回400错误?,post,python-requests,Post,Python Requests,我正在尝试使用政府土地定位应用程序获取已知地块的纬度坐标。该网站是 假设我在搜索字段中输入了土地编号“3520461”并进行搜索。我正在尝试复制post请求,如下所示: import requests url = 'https://www.makani.ae/makaniproxy/Makani.svc/getmakanidatanew' headers = { "Host": "www.makani.ae", "Connection": "k

我正在尝试使用政府土地定位应用程序获取已知地块的纬度坐标。该网站是

假设我在搜索字段中输入了土地编号“3520461”并进行搜索。我正在尝试复制post请求,如下所示:

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错误。我哪里出错了?

你的标题看起来有点不对劲。我建议您将当前标题替换为以下标题:

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"
            }

希望这有帮助

我意识到问题在于有效负载变量中的嵌套字典。归功于。工作代码如下:

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)