首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用Python编写API的POST请求?

如何用Python编写API的POST请求?
EN

Stack Overflow用户
提问于 2017-03-27 19:09:09
回答 1查看 187关注 0票数 1

我有一个MITMproxy数据捕获,结果是下面的输出。

代码语言:javascript
复制
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,将JSON数据附加到查询并设置头部。但是,这会导致一个糟糕的请求错误:<Response [400]> --我怀疑它在since=946677600&options=applymethod,calculatedistance的URL部分--有人能告诉我我做错了什么吗?

这是python代码:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-27 19:42:10

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

代码语言:javascript
复制
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: mobileservice.ge.monster.ch

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43054535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档