Hi there,
I'm creating an iOS 15 app, using the new Swift MusicKit beta.
I've got basic "arbitrary" GET calls working with MusicDataRequest, like: fetch playlists and tracks.
However, I cannot find good docs for adding tracks to a playlist in a user's library.
I guess I could do this the "old way", but then I don't get the super-nice feature of auto dev/user tokens.
So the question is: how to a POST, instead of GET in the following:
let url = URL(string: "https://api.music.apple.com/v1/me/library/playlists/`\(targetPlaylistId)/tracks")
let dataRequest = MusicDataRequest(urlRequest: URLRequest(url: url))
Hi @Kimfucious,
You are trying to use the Add Tracks to a Library Playlist endpoint. If you look at that documentation carefully, you'll see that the body is expected to be a dictionary containing a data
member that points to an array much like the one you're already creating.
So I believe all you're missing is a little structure like:
struct AppleMusicPlaylistPostRequestBody: Codable {
let data: [AppleMusicPlaylistPostRequestItem]
}
Then you can adjust your code as follows:
let requestBody = AppleMusicPlaylistPostRequestBody(
data: tracksToAdd.compactMap {
AppleMusicPlaylistPostRequestItem(id: $0.id, type: "songs")
}
}
[…]
urlRequest.httpBody = try encoder.encode(requestBody)
I hope this helps.
Best regards,