Sample code for 30+ languages & platforms
Java

OpenAI (ChatGPT) Upload File

See more OpenAI ChatGPT Examples

Demonstrate how to upload a file to a OpenAI user's organization.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        // Implements the following CURL command:

        // curl https://api.openai.com/v1/files \
        //   -H "Authorization: Bearer $OPENAI_API_KEY" \
        //   -F purpose="fine-tune" \
        //   -F file="@mydata.jsonl"

        // Use the following online tool to generate HTTP code from a CURL command
        // Convert a cURL Command to HTTP Source Code

        HttpRequest req = new HttpRequest();
        req.setHttpVerb("POST");
        req.setPath("/v1/files");
        req.setContentType("multipart/form-data");
        req.addParam("purpose", "fine-tune");

        // myData.json contains the following:
        // { "prompt": "aa", "completion": "bb" }
        // { "prompt": "cc", "completion": "dd" }
        req.addFileForUpload2("file", "qa_data/json/mydata.jsonl", "application/octet-stream");

        // This is NOT a real key.  Change the "sk-vi...." to your own key.
        req.addHeader("Authorization", "Bearer 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");
        req.addHeader("Expect", "100-continue");

        HttpResponse resp = new HttpResponse();
        try {
            http.httpSReq("api.openai.com", 443, true, req, resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        StringBuilder sbResponseBody = new StringBuilder();
        resp.getBodySb(sbResponseBody);

        JsonObject jResp = new JsonObject();
        jResp.loadSb(sbResponseBody);
        jResp.setEmitCompact(false);

        System.out.println("Response Body:");
        System.out.println(jResp.emit());

        int respStatusCode = resp.getStatusCode();
        System.out.println("Response Status Code = " + respStatusCode);
        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(resp.getHeader());
            System.out.println("Failed.");
            return;
        }

        // Sample JSON response:
        // (Sample code for parsing the JSON response is shown below)

        // {
        //   "id": "file-XjGxS3KTG0uNmNOK362iJua3",
        //   "object": "file",
        //   "bytes": 140,
        //   "created_at": 1613779121,
        //   "filename": "mydata.jsonl",
        //   "purpose": "fine-tune"
        // }

        // Sample code for parsing the JSON response...
        // Use the following online tool to generate parsing code from sample JSON:
        // Generate Parsing Code from JSON

        String id = jResp.stringOf("id");
        String v_object = jResp.stringOf("object");
        int v_bytes = jResp.intOf("bytes");
        int created_at = jResp.intOf("created_at");
        String filename = jResp.stringOf("filename");
        String purpose = jResp.stringOf("purpose");
    }
}