I was messing around with the api mainly just for fun, and was wondering if there was a way to do a iqdb query using the api. I couldn't find any info looking through help:api. any help would be greatly appreciated.
Try doing it in browser with the network tools opened and look at the outgoing requests when you submit the form in that page, an API request is basically the same but done to iqdb_queries.json instead.
Try doing it in browser with the network tools opened and look at the outgoing requests when you submit the form in that page, an API request is basically the same but done to iqdb_queries.json instead.
Hi, thanks for the help. When I do it normally through the browser everything in the network tools looks like expected. Whenever I run my code I get a response 500 and the contents display this message:
"An unexpected error has occurred.\n\nDetails: ActionController::BadRequest exception raised.\n\napp/logical/rack_server_timing.rb:19:in 'RackServerTiming#call'\n"
I'm using python's requests library and my code currently looks like this:
import requests
filepath = 'path_to_file'
with open(filepath, "rb") as f:
payload = {"search[file]": f}
res = requests.post("https://danbooru.donmai.us/iqdb_queries.json", data=payload, auth=("randomweeb12", "api_key"))
print(res)
print(res.content)
Is there something I'm doing wrong? Do i need to send any additional data?
headers = {
"Content-type": "multipart/form-data",
"User-Agent": my_useragent # remember to pass a custom user agent otherwise danbooru will reject your request
}
files = {"search[file]": file_path.open("rb")}
data = {"search[similarity]": 70}
response = requests.post(
"https://danbooru.donmai.us/iqdb_queries.json",
headers=headers,
files=files,
data=data
)
Thanks again for the help. After adjusting my code using your suggestions I now get response 403. After running my code my api code doesn't say it was used so I'm assuming that I'm messing something up with authentication. Any ideas on what I'm missing? Sorry to keep bothering you.
What does the response say? It could be a bad user agent, or lack of credentials, or api key without the correct permissions.
I realized the 403 response was because I just used the user agent from my browser. So I came up with a custom one and now I'm back to getting response 500. Here's the code in full (I added some logging to try and figure out what's going on):
import requests
import logging
# Enabling debugging at http.client level (requests->urllib3->http.client)
# you will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
# the only thing missing will be the response.body which is not logged.
try: # for Python 3
from http.client import HTTPConnection
except ImportError:
from httplib import HTTPConnection
HTTPConnection.debuglevel = 1
logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from requests
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
headers = {
"Content-type": "multipart/form-data",
"User-Agent": "MyApp/1.0", # remember to pass a custom user agent otherwise danbooru will reject your request
}
file_path = 'file_path_here'
files = {"search[file]": open(file_path, "rb")}
data = {"search[similarity]": 70}
res = requests.post(
"https://danbooru.donmai.us/iqdb_queries.json",
headers=headers,
files=files,
data=data,
auth=('randomweeb12', 'api_key_here')
)
print(res)
print(res.content)
Finally managed to get it working. Turns out all i needed to do was send a post request with the file and authentication info. Figured I'd leave my code here as reference material in case anyone else gets confused.
import requests
file_path = 'path_to_file'
with open(file_path, "rb") as f:
payload = {
"search[file]": f,
}
res = requests.post(
"https://danbooru.donmai.us/iqdb_queries.json",
files=payload,
auth=('username_here', 'api_key_here')
)
print(res.content)