[Solved] Server returned HTTP response code: 401 for URL: http://localhost:8989/sonarr/api/command

Dunno if something changed recently or what, but for some reason or another my API call for RescanSeries has stopped working… I did add a “url base” of /sonarr to well sonarr in the general settings a couple days ago because I have been setting up nginx but I don’t think this is what has messed it up (unless it has??)…

Whenever my command goes off I just get the error response that is well the topic name of this post…

This is my current script that is doing the API calls.

// TVDB/TMDB ID
def id = id as int

// Sonarr API Configuration
    def url = new URL('http://localhost:8989')
    def header = ['X-Api-Key': 'APIKEYREDACTED']

    def sonarrSeriesId = new JsonSlurper()
        .parseText(new URL(url, '/api/series')
            .get(header)
            .text)
        .find {
            it.tvdbId == id
        }.id

    println new URL(url, '/api/command').post(
        JsonOutput.toJson(
            [name: 'rescanSeries', seriesId: sonarrSeriesId]
        ).getBytes('UTF-8'),
        'application/json',
        header
    ).text

Okay so testing it it is due to the whole URL base thing, but now I’m confused on how to get it to work with the URL base thing being set…as the whole API wiki never mentions anything about if a URL base is set then to do this instead of that…

I did try to add /sonarr to def url = new URL('http://localhost:8989') so it would be like def url = new URL('http://localhost:8989/sonarr') but that didn’t seem to make a difference as it just threw the same error message as before…

401 = Unauthorized, so either no API key was sent or the wrong one was sent.

Thing is that I am sending the correct API…

things work fine with the URL base not being set on sonarr, but as soon as I add a URL base is when things break, and well I need the URL base set for my reverse proxy to work…

Is the URL that is being hit correctly including the URL Base? If Sonarr redirects the request that is missing the URL Base to include the URL Base the redirected request may not have the correct header. I think the Debug logs should log if it’s being redirected.

this is the debug log

18-5-29 13:29:46.9|Debug|Api|[GET] /api/health: 200.OK (1 ms)
18-5-29 13:29:47.0|Debug|Api|[PUT] /api/config/host: 202.Accepted (123 ms)
18-5-29 13:29:47.0|Debug|Api|[GET] /api/health: 200.OK (0 ms)
18-5-29 13:29:48.9|Debug|Api|[GET] /api/health: 200.OK (0 ms)
18-5-29 13:29:49.0|Debug|Api|[GET] /api/health: 200.OK (2 ms)
18-5-29 13:29:49.2|Debug|Api|[GET] /api/diskspace: 200.OK (278 ms)
18-5-29 13:29:49.5|Debug|Api|[GET] /api/diskspace: 200.OK (251 ms)
18-5-29 13:29:50.2|Debug|Api|[GET] /api/log?page=1&pageSize=50&sortKey=time&sortDir=desc: 200.OK (7 ms)
18-5-29 13:29:50.3|Debug|Api|[GET] /api/log?page=1&pageSize=50&sortKey=time&sortDir=desc: 200.OK (4 ms)
18-5-29 13:29:51.8|Debug|Api|[GET] /api/log/file: 200.OK (2 ms)
18-5-29 13:29:51.9|Debug|Api|[GET] /api/log/file/sonarr.debug.txt: 200.OK (1 ms)
18-5-29 13:30:01.2|Debug|Api|[GET] /api/series: 303.SeeOther (0 ms)
18-5-29 13:30:02.0|Debug|Api|[GET] /api/series: 200.OK (738 ms)
18-5-29 13:30:02.4|Debug|Api|[POST] /api/command: 303.SeeOther (0 ms)
18-5-29 13:30:02.4|Debug|Api|[GET] /api/command: 401.Unauthorized (0 ms)
18-5-29 13:30:04.4|Debug|Api|[GET] /api/log/file: 200.OK (2 ms)
18-5-29 13:30:04.5|Debug|Api|[GET] /api/log/file/sonarr.debug.txt: 200.OK (2 ms)
18-5-29 13:30:17.3|Debug|Api|[GET] /api/command/rescanseries?apikey=(removed) 404.NotFound (1 ms)
18-5-29 13:30:20.2|Debug|Api|[GET] /api/log/file: 200.OK (2 ms)
18-5-29 13:30:02.4|Debug|Api|[POST] /api/command: 303.SeeOther (0 ms)
18-5-29 13:30:02.4|Debug|Api|[GET] /api/command: 401.Unauthorized (0 ms)

is from my script sending out the command.

18-5-29 13:30:17.3|Debug|Api|[GET] /api/command/rescanseries?apikey=(removed) 404.NotFound (1 ms)

is me trying to do http://localhost:8989/sonarr/api/command/rescanseries?apikey=APIKEYREDACTED in my browsers url

Fixed it!

instead of appending/sonarr to the url of def url = new URL('http://localhost:8989') I had to do it like so

    def url = new URL('http://localhost:8989')
    def header = ['X-Api-Key': 'APIKEYREDACTED']

    def sonarrSeriesId = new JsonSlurper()
        .parseText(new URL(url, 'sonarr/api/series')
            .get(header)
            .text)
        .find {
            it.tvdbId == id
        }.id

    println new URL(url, 'sonarr/api/command').post(
        JsonOutput.toJson(
            [name: 'rescanSeries', seriesId: sonarrSeriesId]
        ).getBytes('UTF-8'),
        'application/json',
        header
    ).text

so I had to add it to println new URL(url, '/api/command').post( instead which is weird because why wouldn’t work the other way as well? >.<

May have to do with the path or the URL being replaced when creating the new URL.

Who knows, but glad I have got it solved now as I didn’t want to be forced between either using nginx or using my script.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.