iqdb query from the API

Posted under General

nonamethanks said:

Yes, you just do a POST to https://danbooru.donmai.us/iqdb_queries.

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?

You're not setting the headers. Try it like this:

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
)

nonamethanks said:

You're not setting the headers. Try it like this:

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.

nonamethanks said:

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)

And here's the output:

DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): danbooru.donmai.us:443
send: b'POST /iqdb_queries.json HTTP/1.1\r\nHost: danbooru.donmai.us\r\nUser-Agent: MyApp/1.0\r\nAccept-Encoding: gzip, deflate, zstd\r\nAccept: */*\r\nConnection: keep-alive\r\nContent-type: multipart/form-data\r\nContent-Length: 4641177\r\nAuthorization: Basic <snip>==\r\n\r\n'
send: b'--<snip>\r\nContent-Disposition: form-data ...
reply: 'HTTP/1.1 500 Internal Server Error\r\n'
header: Date: Thu, 18 Dec 2025 20:41:53 GMT
header: Content-Length: 156
header: Connection: keep-alive
header: Server: cloudflare
header: Nel: {"report_to":"cf-nel","success_fraction":0.0,"max_age":604800}
header: Via: 108.181.143.72, gura, ame/danbooru-778c87465d-7lftq
header: Strict-Transport-Security: max-age=31536000; preload
header: X-Frame-Options: sameorigin
header: cf-cache-status: DYNAMIC
header: Report-To: {"group":"cf-nel","max_age":604800,"endpoints":[{"url":"https://a.nel.cloudflare.com/report/v4?s=%2B7jqLurJ5BZwwcHZK8%2BF%2BSAoXtjPQjpMMo4lL7MqRXlPoU3lNn%2BLCsWxHnPdiFvRMr1W0K7DvkdEk42Bs%2FwO2qaeajx4UwUSFJjk1IwX03Ad"}]}
header: CF-RAY: 9b017bbf6abce82a-ORD
header: alt-svc: h3=":443"; ma=86400
DEBUG:urllib3.connectionpool:https://danbooru.donmai.us:443 "POST /iqdb_queries.json HTTP/1.1" 500 156
<Response [500]>
b"An unexpected error has occurred.\n\nDetails: ActionController::BadRequest exception raised.\n\napp/logical/rack_server_timing.rb:19:in 'RackServerTiming#call'\n"

Updated by a moderator

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