0
votes

I am trying to create a playlist using the Spotify API, and I am writing the POST request to the Spotify API endpoint in Java. I have also included every available scope from Spotify when I retrieve my access token. This is returning a response with an error message of:

{"error":{"message":"Error parsing JSON.","status":400}}

Here is what I have:

String http = "https://api.spotify.com/v1/users/" + userId + "/playlists";

CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(http);

JsonObject entityObj = new JsonObject();
JsonObject dataObj = new JsonObject();
dataObj.addProperty("name", "title");
dataObj.addProperty("public", "false");
entityObj.add("data", dataObj);

String dataStringify = GSON.toJson(entityObj);
StringEntity entity = new StringEntity(dataStringify);
post.setEntity(entity);

post.setHeader("Authorization", "Bearer " + accessToken);
post.setHeader("Content-Type", "application/json");

CloseableHttpResponse response = client.execute(post);

System.out
    .println("Response Code : " + response.getStatusLine().getStatusCode());
String resp = EntityUtils.toString(response.getEntity());
JSONObject responseObj = new JSONObject(resp);
System.out.println(responseObj);

client.close();

Please let me know if you have any insights into what is wrong.

1

1 Answers

0
votes

I am assuming you are using the org.json library as well as Google's Gson library. Using both doesn't make sense in this context. You won't need

String dataStringify = GSON.toJson(entityObj);

as entity Object already is a JSON Object. entityObj.toString() should be enough. The current JSON Data you are sending looks like this:

{
 "data":
   {
    "name":"title",
    "public":"false"
   }
}

Spotify ask for an JSON Object like this:

{
  "name": "New Playlist",
  "public": false
}

You only have to send the Data Object dataObj.