Announcements

Help Wizard

Step 1

NEXT STEP

FAQs

Please see below the most popular frequently asked questions.

Loading article...

Loading faqs...

VIEW ALL

Ongoing Issues

Please see below the current ongoing issues which are under investigation.

Loading issue...

Loading ongoing issues...

VIEW ALL

HTML page instead of response

Solved!

HTML page instead of response

Hello, I would like to write an application in Kotlin that uses the Spotify API. To start with I would like to just understand what is going on and wrote the following code:

 

val client = HttpClient(CIO)
suspend fun main(args: Array<String>) {
val otvet:String=client.post("https://accounts.spotify.com/api/token"){
headers{"Content-Type: application/x-www-form-urlencoded"}
setBody("grant_type=client_credentials&client_id=000&client_secret=000")
}.body()
println(otvet)
}

 

(I removed the key and secret key)
But instead of what was described here: https://developer.spotify.com/documentation/web-api/tutorials/getting-started

I got an HTML page that opens if I open https://accounts.spotify.com/api/token directly in the browser. How to fix it?

Reply

Accepted Solutions
Marked as solution

Howling hello to you A_CH_V!
It seems like you’re facing an issue where the Spotify API is returning an HTML page instead of the expected response. This could be due to incorrect request parameters or headers. Here are a few things to check:

1. URL Encoding: Make sure the parameters in the setBody function are URL encoded properly.
2. Header Formatting: Ensure that the header is in the correct format. In this case, the “Content-Type” header should be separated by a colon.
3. Client Credentials: Double-check that the client ID and client secret you’re using are correct.

 

Here’s a corrected version of your code with these considerations:

import io.ktor.client.HttpClient
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.request.headers
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.ContentType
import io.ktor.http.content.TextContent

suspend fun main(args: Array<String>) {
    val clientId = "YOUR_CLIENT_ID"
    val clientSecret = "YOUR_CLIENT_SECRET"

    val client = HttpClient {
        install(JsonFeature)
    }

    val response = client.post<String>("https://accounts.spotify.com/api/token") {
        headers {
            append("Content-Type", ContentType.Application.FormUrlEncoded.toString())
        }
        body = TextContent(
            "grant_type=client_credentials&client_id=$clientId&client_secret=$clientSecret",
            ContentType.Application.FormUrlEncoded
        )
    }

    println(response)
}

Replace "YOUR_CLIENT_ID" and "YOUR_CLIENT_SECRET" with your actual Spotify API client ID and secret. Ensure that the content type and request body are formatted correctly. If the issue persists, double-check your Spotify API credentials and request format.

I’ll be barking up the wrong tree without any updates,

 

-Prague the Dog 

View solution in original post

1 Reply
Marked as solution

Howling hello to you A_CH_V!
It seems like you’re facing an issue where the Spotify API is returning an HTML page instead of the expected response. This could be due to incorrect request parameters or headers. Here are a few things to check:

1. URL Encoding: Make sure the parameters in the setBody function are URL encoded properly.
2. Header Formatting: Ensure that the header is in the correct format. In this case, the “Content-Type” header should be separated by a colon.
3. Client Credentials: Double-check that the client ID and client secret you’re using are correct.

 

Here’s a corrected version of your code with these considerations:

import io.ktor.client.HttpClient
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.request.headers
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.ContentType
import io.ktor.http.content.TextContent

suspend fun main(args: Array<String>) {
    val clientId = "YOUR_CLIENT_ID"
    val clientSecret = "YOUR_CLIENT_SECRET"

    val client = HttpClient {
        install(JsonFeature)
    }

    val response = client.post<String>("https://accounts.spotify.com/api/token") {
        headers {
            append("Content-Type", ContentType.Application.FormUrlEncoded.toString())
        }
        body = TextContent(
            "grant_type=client_credentials&client_id=$clientId&client_secret=$clientSecret",
            ContentType.Application.FormUrlEncoded
        )
    }

    println(response)
}

Replace "YOUR_CLIENT_ID" and "YOUR_CLIENT_SECRET" with your actual Spotify API client ID and secret. Ensure that the content type and request body are formatted correctly. If the issue persists, double-check your Spotify API credentials and request format.

I’ll be barking up the wrong tree without any updates,

 

-Prague the Dog 

Suggested posts