2017-03-27 2 views
1

다음과 같은 결과가 나오는 MITMproxy 데이터 캡처가 있습니다.파이썬에서 API에 대한 POST 요청을 공식화하려면 어떻게해야합니까?

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

POST-URL, POST에 대한 JSON 데이터 첨부 및 헤더 설정과 관련하여 파이썬에서이를 복제하려고했습니다. 그러나 이렇게하면 잘못된 요청 오류가 발생합니다. <Response [400]> URL 부분의 어딘가에있는 것으로 의심됩니다. since=946677600&options=applymethod,calculatedistance 내가 뭘 잘못했는지 말해 줄 수 있습니까?

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) 
+0

보십시오'requests.post (url, json = payload, headers = headers)' –

+0

고마워요. 이 결과는 이제 500 오류가 발생합니다. –

답변

1

가능한 첫 번째로 캡처에 관해서는 동일한 매개 변수를 사용하여 쿼리를 실행 해보십시오 :

는 파이썬 코드입니다. 재판까지 200

에 캡처

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() 

응답 변경에서 헤더의 전체 집합을 실행하고 오류 때 당신은 다음 필요한 헤더 x-domain: mobileservice.ge.monster.ch 것을 확인할 수 있습니다.

+0

입력 해 주셔서 대단히 감사드립니다. 나는 전체 헤더 1 : 1을 질의에 포함해야했지만, 답은 목표 바로 앞에 나를 놓았다. –

관련 문제