我正在尝试为POST请求naturalHR API中的候选人发送文件:我已经尝试过使用POSTMAN
进行相同的请求,并且效果很好。但是,当我尝试使用python集成API的POST
请求以附加文件时,我收到一个错误,即cv
参数应为文件(其API错误响应)。
源代码:
from pprint import pprint
import json
import requests
import urllib.request
headers = {
'accept': 'application/json',
'Authorization': api_key,
'Host': 'api02.naturalhr.net',
'Referer': 'https://api02.naturalhr.net/api/documentation',
'Content-type': 'multipart/form-data',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36'
}
payLoad = dict()
payLoad["firstname"] = json_of_vals['firstname']
payLoad["surname"] = json_of_vals['surname']
payLoad["email"] = json_of_vals['email']
payLoad["cv"] = "Path/To/PDF_File"
files = {'file': "outfilename.pdf"}
api_url = "https://api02.naturalhr.net/api/v1/candidate"
res = requests.post(api_url, files=files, headers=headers, data=request_data)
print(res.content)
请不要将其标记为此处已回答的问题的重复,因为我已经通过使用files作为请求的参数进行了测试,例如:
res = requests.post(api_url, files=files, headers=headers, data=request_data)
编辑:我尝试过的答案:
Using Python Requests to send file and JSON in single request