Python api call headers problem

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:

import requests
url = ‘https://api.wanikani.com/v2/
headers = {‘Authorization’: ‘Bearer my-v2-token’}

req = requests.get(url, headers=headers)
print(req) //returns 404

Thanks in advance!!

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)

The result is

{'data': {'current_vacation_started_at': None,
          'id': 'b0e81df9-ed27-4880-8819-fe822506fd63',
          'level': 28,
          'preferences': {'default_voice_actor_id': 1,
                          'lessons_autoplay_audio': True,
                          'lessons_batch_size': 5,
                          'lessons_presentation_order': 'ascending_level_then_subject',
                          'reviews_autoplay_audio': True,
                          'reviews_display_srs_indicator': True},
          'profile_url': 'https://www.wanikani.com/users/trunklayer',
          'started_at': '2016-03-14T19:31:05.862886Z',
          'subscription': {'active': True,
                           'max_level_granted': 60,
                           'period_ends_at': None,
                           'type': 'lifetime'},
          'username': 'trunklayer'},
 'data_updated_at': '2020-08-26T14:15:07.799281Z',
 'object': 'user',
 'url': 'https://api.wanikani.com/v2/user'}

You didn’t pass your token and endpoint, that’s why you get 404.

By the way, you should try to see what the api returns by:

print(req.json())

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.

Yeah, 404 is because of the missing endpoint. But they should get the error from the response json to ve sure.

@d-hermit already pointed it out… the URL needs to be https://api.wanikani.com/v2/user.

It’s currently trying to get the base-url for the V2 API.

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?

I only ever used a context manager of Session.

Probably, not, but I don’t know the requests very good, so I decided to be on the safe side :sweat_smile:

May I know what you guys are doing with the API on Python?

We’re skipping wk levels… but don’t tell anyone pls.

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 :sweat_smile:
So, I always try to use any opportunity to practice :sweat_smile:

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.