I am trying to create a new playlist folder in Apple Music using MusicKit on iOS 16. However, I am receiving an Internal Service Error when sending the request.
This is the code:
let urlString = "https://api.music.apple.com/v1/me/library/playlist-folders"
let url = URL(string: urlString)!
let body = LibraryPlaylistFolderCreationRequest(
attributes: .init(
name: "test-playlist-folder"),
relationships: .init(
parent: .init(
data: [.init(id: "playlistsroot", type: "library-playlist-folders")])))
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
urlRequest.httpBody = try JSONEncoder().encode(body)
let request = MusicDataRequest(urlRequest: urlRequest)
let response = try await request.response()
let responseString = String(data: response.data, encoding: .utf8)
print(responseString)
When I print the request body using the following code snippet, I get the output as seen below:
let jsonBody = try JSONEncoder().encode(body)
print(String(data: jsonBody, encoding: .utf8))
Output of the above snippet:
Optional("{\"attributes\":{\"name\":\"test-playlist-folder\"},\"relationships\":{\"parent\":{\"data\":[{\"id\":\"playlistsroot\",\"type\":\"library-playlist-folders\"}]}}}")
Which should conform to the JSON scheme provided in the docs: https://developer.apple.com/documentation/applemusicapi/libraryplaylistfoldercreationrequest
And this is the error output:
[DataRequesting] Failed to perform MusicDataRequest.Context(
url: "https://api.music.apple.com/v1/me/library/playlist-folders",
currentRetryCounts: [.other: 1]
) with MusicDataRequest.Error(
status: 500,
code: 50000,
title: "Internal Service Error",
detailText: "",
id: "VK23VIZ6AAM5IVP4GIFQ6VXPLU",
originalResponse: MusicDataResponse(
data: 111 bytes,
urlResponse: <NSHTTPURLResponse: 0x000000028042b0c0>
)
).
After a lot of trial and error, I don't think I know what the underlying issue is. Is MusicDataRequest not designed to issue POST requests? Is it something else?
Kind regards!