1

I am trying to send a file for a candidate in POST request naturalHR API: I have tried the same request using POSTMAN and it worked fine. But when i try to integrate the API's POST request using python to attach the file I am getting an error that It cv parameter should be a file(its API error response).

Source Code:

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)

Please dont mark this as a duplicate to a question here which is already been answered because I have tested it by using files as request's argument like:

res = requests.post(api_url, files=files, headers=headers, data=request_data)

Edited: The answer which I have tried:

Using Python Requests to send file and JSON in single request

GigaByte
  • 700
  • 6
  • 21
  • 1
    Can you please check out this answer if that works or not. https://stackoverflow.com/questions/68477/send-file-using-post-from-a-python-script. Also just on side note you can add which answers you already tried and not worked. It can help others debug. :) – Poojan Oct 04 '19 at 19:20
  • still getting an issue. In postman, I am sending data under form-data section – GigaByte Oct 04 '19 at 20:16
  • but in python post request when i try by using data arguement i am getting API error for required params – GigaByte Oct 04 '19 at 20:17
  • 1
    Try {'file': open("outfilename.pdf","rb")} and payLoad["cv"]=open("Path/To/PDF_File","rb") – Mrugesh Kadia Oct 09 '19 at 05:57
  • Yes, it worked. – GigaByte Oct 14 '19 at 20:24
  • There was problem in headers as if I send accept header it will not accept the file as file is not in JSON one. – GigaByte Oct 14 '19 at 20:26

1 Answers1

0

I was adding a header

'accept': 'application/json'

Which should not be there, I tried it using only user-agent and an API-key and it worked perfectly fine as per requirements.

Corrected Code:

from pprint import pprint
import json
import requests
import urllib.request
headers = {
    'Authorization': api_key,
    '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']

files = {'file': "PATH/TO/FILE/FROM/LOCAL/DRIVE"}

api_url = "https://api02.naturalhr.net/api/v1/candidate"
res = requests.post(api_url, headers=headers, data=payLoad, files=files)
print("Status Code is: ", res.status_code)
print("Returned JSON Response is:\n")
pprint(res.text)
GigaByte
  • 700
  • 6
  • 21