Android™ 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"

Android™ Example

// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    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) {
        Log.i(TAG, "ConnectFailReason: " + String.valueOf(rest.get_ConnectFailReason()));
        Log.i(TAG, 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) {
        Log.i(TAG, 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");

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}

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"
}