Java Stripe: Create a File Upload

Back to Index

Uploads a file to Stripe.

Documentation: https://stripe.com/docs/api/curl#create_file_upload

CURL Command

curl https://files.stripe.com/v1/files \
   -u STRIPE_SECRET_KEY: \
   -F purpose=dispute_evidence \
   -F file="@/path/to/a/file.jpg"

Java Example

import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    CkRest rest = new CkRest();
    boolean success;

    //  URL: https://files.stripe.com/v1/files
    boolean bTls = true;
    int port = 443;
    boolean bAutoReconnect = true;
    success = rest.Connect("files.stripe.com",port,bTls,bAutoReconnect);
    if (success != true) {
        System.out.println("ConnectFailReason: " + rest.get_ConnectFailReason());
        System.out.println(rest.lastErrorText());
        return;
        }

    rest.SetAuthBasic("STRIPE_SECRET_KEY","");

    rest.put_PartSelector("1");
    rest.AddHeader("Content-Disposition","form-data; name=\"purpose\"");
    rest.SetMultipartBodyString("dispute_evidence");

    rest.put_PartSelector("2");
    CkStream fileStream2 = new CkStream();
    fileStream2.put_SourceFile("/path/to/a/file.jpg");
    rest.AddHeader("Content-Disposition","form-data; name=\"/path/to/a/file.jpg\"; filename=\"/path/to/a/file.jpg\"");
    rest.AddHeader("Content-Type","image/jpeg");
    rest.SetMultipartBodyStream(fileStream2);

    rest.put_PartSelector("0");

    rest.AddHeader("Expect","100-continue");

    String strResponseBody = rest.fullRequestMultipart("POST","/v1/files");
    if (rest.get_LastMethodSuccess() != true) {
        System.out.println(rest.lastErrorText());
        return;
        }

    CkJsonObject jsonResponse = new CkJsonObject();
    jsonResponse.Load(strResponseBody);

    String id;
    String object;
    int created;
    String filename;
    String purpose;
    int size;
    String type;
    String url;

    id = jsonResponse.stringOf("id");
    object = jsonResponse.stringOf("object");
    created = jsonResponse.IntOf("created");
    filename = jsonResponse.stringOf("filename");
    purpose = jsonResponse.stringOf("purpose");
    size = jsonResponse.IntOf("size");
    type = jsonResponse.stringOf("type");
    url = jsonResponse.stringOf("url");
  }
}

Sample JSON Response Body

{
  "id": "file_1BnEEuGswQrCoh0XqB3XkqAg",
  "object": "file_upload",
  "created": 1516661888,
  "filename": "path",
  "purpose": "sigma_scheduled_query",
  "size": 500,
  "type": "csv",
  "url": "https://stripe-upload-api.s3.amazonaws.com/uploads/file_1BnEEuGswQrCoh0XqB3XkqAg?AWSAccessKeyId=KEY_ID\u0026Expires=TIMESTAMP\u0026Signature=SIGNATURE"
}