Hi, im using python for the first time and tried to make a succesfull api call. I cant seem to make the headers work since i am getting a 404. This is what i tried:
Edit: i’m sorry about the api key that wasnt there. Seems like these “<>” characters are hidden when they have text inside, which i used and didnt notice…
The problem was the endpoint which i forgot to set. I’ve never felt so dumb in a long time.
I don’t know anything about using Python to make requests, but you haven’t specified an endpoint, have you? Also no API key, unless you removed it for the forum post
Well, don’t know if that would help but here’s how I’m using Python to access WK API:
import requests
import json
from pprint import pprint
address = 'https://api.wanikani.com/v2'
#Obviously, this is not the token I'm using in the actual code
token = 'NOT_THE_ACTUAL_TOKEN'
headers = {
'Authorization': f'Bearer {token}'
}
endpoint = 'user'
with requests.get(f'{address}/{endpoint}', headers=headers) as r:
response = r.json()
pprint(response)
Not passing a token would result in a 401, a 404 is when a resource can’t be found.
I suspect that OP just removed their key from their code before posting, and is just sending requests to the base URL, instead of specifying an endpoint.
Didn’t know Response was a context manager before. As I understand it can only be useful if you get a streaming body. Curious, is there a particular reason to use it here?
Well, for me it’s just some practice.
I’ve only started studying Python in the beginning of this year and I find it extremely useful, but I still have loads and loads to learn
So, I always try to use any opportunity to practice
I find context managers useful whenever there’s something that needs cleanup. At a guess, I’d say that leaving the with block would close any connection and flush any stream, freeing up resources. I don’t know if you gain anything doing things this way, but it certainly doesn’t hurt.
Thanks for the replies y’all. Sorry for the late reply, i was very busy. I’ve seen all the comments about the missing api key, it wasnt there because i used these “<>” characters which seem to hide the text inside. Sorry for the misunderstanding. I got it working now, it was indeed the endpoint that i forgot and i feel very dumb right now.