Getting 400 when uploading video file programmatically

I tried to upload a video file programmatically to Twitch. I following the instruction on https://dev.twitch.tv/docs/video-upload/, and was successfully created a video using the API, however, when uploading it, I got error code 400, but no error message. My test code is attached. Can anyone give me some insight on what is missing? Many thanks!

// HTTP POST request
private void uploadVideo(String uploadUrl, String token, String videoFile) throws Exception {

	String boundary = createBoundary();
	String LINE_FEED = "\r\n";

	String url = uploadUrl + "?index=1&upload_token=" + URLEncoder.encode(token, "UTF-8");

	try {

	    URL putUrl = new URL(uploadUrl + "?index=1&upload_token=" + token);
	    HttpURLConnection httpCon = (HttpURLConnection) putUrl.openConnection();
	    //httpCon.setRequestProperty("Content-Length", "" + data.length);

	    httpCon.setUseCaches(false);
        httpCon.setDoOutput(true);
        httpCon.setDoInput(true);
        httpCon.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        httpCon.setRequestProperty("User-Agent", "Java IPFS CLient");

	    httpCon.setRequestMethod("PUT");


	    // set some connection properties
	    OutputStream output = httpCon.getOutputStream();

	    PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, CHARSET), true);

	    writer.append("--" + boundary).append(LINE_FEED);

        writer.append("Content-Disposition: file; filename=\"" + videoFile + "\"").append(LINE_FEED);


        writer.append("Content-Type: application/octet-stream").append(LINE_FEED);
        writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
        writer.append(LINE_FEED);
        writer.flush();



        InputStream inputStream = new FileInputStream(videoFile);
        byte[] buffer = new byte[4096];
        int r;
        while ((r = inputStream.read(buffer)) != -1)
            output.write(buffer, 0, r);
        output.flush();
        inputStream.close();

        writer.append(LINE_FEED);
        writer.flush();


        StringBuilder b = new StringBuilder();

        writer.append("--" + boundary + "--").append(LINE_FEED);
        writer.close();

        int status = httpCon.getResponseCode();
        if (status == HttpURLConnection.HTTP_OK) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    httpCon.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                b.append(line);
            }
            reader.close();
            httpCon.disconnect();
        } else {
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        httpCon.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    b.append(line);
                }
                reader.close();
            } catch (Throwable t) {}
            throw new IOException("Server returned status: " + status + " with body: "+b.toString() + " and Trailer header: "+httpCon.getHeaderFields().get("Trailer"));
        }

        System.out.println("Server response status code is " + status + "; message is " + b.toString());

	}
	catch(Exception e)
	{
		e.printStackTrace();
	}


}

Try part=1 instead of index=1 in your URL. Curious if that’s the problem.

OMG, yes, part=1 works, I got response code of 200. However, I don’t see the video on my channel. Does it take sometime for it to show up?

A couple things: Did you upload all the parts? Have you sent the complete call to finish the upload? https://dev.twitch.tv/docs/video-upload/#completing-the-video-upload

The video is only 13MB, so only one part is needed. I did send the “Complete” request, and I got 200 response code. However, I saw this on the website: “There was an error with your video, Cancel upload, or try uploading again.” Any suggestions?

I was able to upload the same video file from Twitch UI.

I see the process spinner running for a while before video shows up. However from API, it was fairly fast for the upload and complete API to return. I am wondering if there is anything I missed. Thank a ton for your advice.

I solved it, everything works, thank you very much!

Someone from Twitch needs to update the API doc. https://dev.twitch.tv/docs/video-upload/#completing-the-video-upload, there is no way developer knows to use part=xxx, instead of index=xxx. I appreciate so much of your help.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.