0

I am new to using HTTP and I have questions about writing a file and another value to an HTTP Post request in Java. I am using an public API provided by a company called Mojang to write what is known as a "skin" (a png file) to the game Minecraft for player character modles. Here is the documentation of how to use this public API for reference:https://wiki.vg/Mojang_API#Upload_Skin

Here is the code I have written. When ran I get the 415 HTTP Response code (which I assume is "unsupported media type"). Any suggestions on what I am doing wrong and how I can fix this? I found other stack overflow issues for uploading files but I need to also add a value called "variant={classic or slim}". I am a little lost on how to make all of this work. Any help is much appreciated.

(I could not get the code to properally format in the code sample using ' ', it is in a javascript snippet)

    public static void uploadSkin(String accessToken, String variant, File file) throws IOException {

    URL url = new URL("https://api.minecraftservices.com/minecraft/profile/skins");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setDoOutput(true);
    con.setRequestMethod("POST");
    con.setRequestProperty("Authorization", "Bearer " + accessToken); // The access token is provided after an
                                                                        // authentication request has been send, I
                                                                        // have done this sucessfully in another
                                                                        // method and am passing it in here

    con.addRequestProperty("variant", variant);
    
    OutputStream outputStream = con.getOutputStream();
    PrintWriter writer = new PrintWriter(new OutputStreamWriter(con.getOutputStream(), "utf-8"), true);
    String boundary = "===" + System.currentTimeMillis() + "===";
    String fileName = file.getName();
    String LINE_FEED = "\r\n";
    String fieldName = "file";

    writer.append("--" + boundary).append(LINE_FEED);
    writer.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"")
            .append(LINE_FEED);
    writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED);
    writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
    writer.append(LINE_FEED);
    writer.flush();

    FileInputStream inputStream = new FileInputStream(file);

    byte[] buffer = new byte[4096];
    int bytesRead = -1;

    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }
    outputStream.flush();
    inputStream.close();

    writer.append(LINE_FEED);
    writer.flush();
}
2
  • You need to send a request with Content-Type: multipart/form-data. I would strongly advise to use an HTTP client library for this. Commented Jul 29, 2021 at 21:21
  • Okay, thank you, I will give that a try and look for a client library. Commented Jul 29, 2021 at 21:26

1 Answer 1

1

Alright, found a solution to the problem. Using this maven dependency:

    <!-- https://mvnrepository.com/artifact/org.jodd/jodd-http -->
    <dependency>
        <groupId>org.jodd</groupId>
        <artifactId>jodd-http</artifactId>
        <version>5.0.2</version>
    </dependency>

And then this:

        HttpResponse response = HttpRequest.post("https://api.minecraftservices.com/minecraft/profile/skins")
            .header("Authorization", "Bearer " + accessToken).header("Content-Type", "multipart/form-data")
            .form("variant", variant).form("file", file).send();

I was able to get it to work. Hope this is helpful to anyone that needs to upload a Skin Png file to Minecraft.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.