Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中为API制定POST请求?_Python_Api_Mitmproxy - Fatal编程技术网

如何在Python中为API制定POST请求?

如何在Python中为API制定POST请求?,python,api,mitmproxy,Python,Api,Mitmproxy,我有一个MITMproxy数据捕获,产生以下输出 POST https://gateway.monster.com/seeker/mobile/jobs/search/solr?since=946677600&options=applymethod,calculatedistance Accept: application/json Content-Type: appli

我有一个MITMproxy数据捕获,产生以下输出

POST
https://gateway.monster.com/seeker/mobile/jobs/search/solr?since=946677600&options=applymethod,calculatedistance

Accept:           application/json                                            
Content-Type:     application/json                                            
User-Agent:       monster/2.12.0/800/iOS;10;iPhone-5s                      
Accept-Encoding:  gzip, deflate                                               
x-domain:         mobileservice.ge.monster.ch                                 
x-brand:          1                                                           
x-ver:            2.12.0                                                      
x-uid:            64e0a64c-ddb5-489c-ab5f-0dec9fed1066                        
x-device:         11                                                          
x-device-model:   iPhone 5s                                                    
x-os-ver:         10                                                      
Content-Length:   313                                                         
Host:             gateway.monster.com

JSON
{
"AgentId": 0,
"CompanyName": "",
"CompanyXCode": "",
"Country": "US",
"Filters": {
    "CareerLevels": [],
    "EducationLevels": [],
    "JobBoardIds": [],
    "JobTypes": [],
    "PostingDuration": -1,
    "YearsOfExp": []
},
"JobTitle": "",
"Keywords": "business",
"Latitude": 0.0,
"Longitude": 0.0,
"Page": 1,
"PageSize": 25,
"Radius": 10,
"Sort": "dt.rv.di",
"Where": "Zurich"
}
我尝试用python进行如下复制:POST-URL、将JSON数据附加到查询并设置标题。 但是,这会导致错误的请求错误:
我怀疑它在URL部分的某个地方:
since=946677600&options=applymethod,calculatedInstance
谁能告诉我我做错了什么

这是python代码:

import requests
import json
import time
from datetime import datetime
import os
from random import randint

#query

url = 'https://gateway.monster.com/seeker/mobile/jobs/search/solr?  
since=946677600&options=applymethod,calculatedistance'
payload = {
  "AgentId": 0,
  "CompanyName": "",
  "CompanyXCode": "",
  "Country": "US",
  "Filters": {
      "CareerLevels": [],
      "EducationLevels": [],
      "JobBoardIds": [],
      "JobTypes": [],
      "PostingDuration": -1,
      "YearsOfExp": []
  },
   "JobId": "",
   "JobTitle": "",
  "Keywords": "software developer",
  "Latitude": 0.0,
  "LocationDescription": "new york",
  "Longitude": 0.0,
  "Page": 1,
  "PageSize": 25,
  "Radius": 20,
  "Sort": "dt.rv.di",
  "Where": "new york"
}

headers = {'content-type': 'application/json'}

# Get a copy of the default headers that requests would use
headers = requests.utils.default_headers()

# Update the headers with your custom ones
# You don't have to worry about case-sensitivity with
# the dictionary keys, because default_headers uses a custom
# CaseInsensitiveDict implementation within requests' source code.
#userAgents 
headers.update(
  {
      'User-Agent': 'Monster/2.12.0/800/iOS;10.2.1;iPhone-6',
  }
)

 #get the query result from the url with UA set for a ALL jobs in current location
response = requests.post(url, data=json.dumps(payload), headers=headers)
print(response)

首先尝试使用与捕获尽可能相同的参数运行查询。使用捕获中的完整标题集运行时

headers = requests.utils.default_headers()

headers_str = """Accept:           application/json                                            
Content-Type:     application/json                                            
User-Agent:       monster/2.12.0/800/iOS;10;iPhone-5s                      
Accept-Encoding:  gzip, deflate                                               
x-domain:         mobileservice.ge.monster.ch                                 
x-brand:          1                                                           
x-ver:            2.12.0                                                      
x-uid:            64e0a64c-ddb5-489c-ab5f-0dec9fed1066                        
x-device:         11                                                          
x-device-model:   iPhone 5s                                                    
x-os-ver:         10                                                      
Content-Length:   313                                                         
Host:             gateway.monster.com"""

for l in txt.splitlines():
    k, v = l.split(':')
    headers[k] = v.strip()
响应变为200


通过反复试验,您可以确定所需的头是
x-domain:mobileseservice.ge.monster.ch

尝试
requests.post(url,json=payload,headers=headers)
谢谢。这将导致500个错误。非常感谢您的输入。我必须在查询中包含完整的标题1:1,但您的回答将我放在了目标前面