I have been trying to export offer file using Python for Bol Retailer API According to the official docs on request an offer file export
I have include all the headers and formats but it throws a 400 Bad Request
400
Bad Request
b'{\n "type" : "https://api.bol.com/problems",\n "title" : "Bad Request",\n "status" : 400,\n "detail" : "The supplied content-type media type is not supported.",\n "host" : "Instance-111",\n "instance" : "https://api.bol.com/retailer/offers/export"\n}'
Here is a minimal example from my code
import base64
import requests
import json
import time
class BolService:
def __init__(self, _id, secret):
self.host = "https://api.bol.com"
self.__header = {
"Accept": "application/vnd.retailer.v7 json",
"Content-Type": "N/A",
"Authorization": "Bearer " self.__get_token(_id, secret)
}
def __get_token(self, _id, secret) -> str:
creds = (_id ":" secret).encode('ascii') #creds : ascii bytes
creds_b64_b = base64.b64encode(creds) #creds : base64 bytes
creds_b64 = creds_b64_b.decode('ascii') #creds : base64 string
header = {
"Authorization":"Basic " creds_b64
}
link = "https://login.bol.com/token?grant_type=client_credentials"
response = requests.post(link, headers=header)
response_data = json.loads(response.content.decode())
return response_data['access_token']
def get_offer_file(self):
path = f"/retailer/offers/export"
new_header = self.__header.copy()
new_header["format"] = "CSV"
response = requests.post(self.host path, headers=new_header)
return response
Note: I have also tried changing the "Content-Type"
in self.__header
to "application/vnd.retailer.v7 json"
, I have also changed the same to add csv using "application/vnd.retailer.v7 json csv"
or "application/vnd.retailer.v7 csv"
. I have also tried adding self.__header['Content-Type'] = 'text/csv'
but nothing seems to work it keeps on throwing the same Bad Request. I have also tried using the v6 of the API instead of v7 but same issue.
I know this is something that should be dealt with the customer service of Bol but they their service is too pathetic to even give a simple reply. Also as of August 2022 their site which details API issues is down. Maybe if someone with experience can help here. I don't think I am missing anything here. Please let me know.
CodePudding user response:
So I was able to sucessfully make the POST request.
1st what I did was change the "Content-Type"
in self.__header
to "application/vnd.retailer.v7 json"
so the header now looks like this
self.__header = {
"Accept": "application/vnd.retailer.v7 json",
"Content-Type": "application/vnd.retailer.v7 json",
"Authorization": "Bearer " self.__get_token(_id, secret)
}
Since we require the content type in JSON format so we have to include a JSON body by dumping our dictionary content using json.dumps
So the get_offer_file method now looks like with {"format":"CSV"}
as the body
def get_offer_file(self):
path = f"/retailer/offers/export"
response = requests.post(self.host path, headers=self.__header, data=json.dumps({"format":"CSV"}))
return response
Here is the full code:
import base64
import requests
import json
class BolService:
def __init__(self, _id, secret):
self.host = "https://api.bol.com"
self.__header = {
"Accept": "application/vnd.retailer.v7 json",
"Content-Type": "application/vnd.retailer.v7 json",
"Authorization": "Bearer " self.__get_token(_id, secret)
}
def __get_token(self, _id, secret) -> str:
creds = (_id ":" secret).encode('ascii') #creds : ascii bytes
creds_b64_b = base64.b64encode(creds) #creds : base64 bytes
creds_b64 = creds_b64_b.decode('ascii') #creds : base64 string
header = {
"Authorization":"Basic " creds_b64
}
link = "https://login.bol.com/token?grant_type=client_credentials"
response = requests.post(link, headers=header)
response_data = json.loads(response.content.decode())
return response_data['access_token']
def get_offer_file(self):
path = f"/retailer/offers/export"
response = requests.post(self.host path, headers=self.__header, data=json.dumps({"format":"CSV"}))
return response