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

C Example

#include <C_CkRest.h>
#include <C_CkStream.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    HCkRest rest;
    BOOL success;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkStream fileStream2;
    const char *strResponseBody;
    HCkJsonObject jsonResponse;
    const char *id;
    const char *object;
    int created;
    const char *filename;
    const char *purpose;
    int size;
    const char *type;
    const char *url;

    rest = CkRest_Create();

    //  URL: https://files.stripe.com/v1/files
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRest_Connect(rest,"files.stripe.com",port,bTls,bAutoReconnect);
    if (success != TRUE) {
        printf("ConnectFailReason: %d\n",CkRest_getConnectFailReason(rest));
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        return;
    }

    CkRest_SetAuthBasic(rest,"STRIPE_SECRET_KEY","");

    CkRest_putPartSelector(rest,"1");
    CkRest_AddHeader(rest,"Content-Disposition","form-data; name=\"purpose\"");
    CkRest_SetMultipartBodyString(rest,"dispute_evidence");

    CkRest_putPartSelector(rest,"2");
    fileStream2 = CkStream_Create();
    CkStream_putSourceFile(fileStream2,"/path/to/a/file.jpg");
    CkRest_AddHeader(rest,"Content-Disposition","form-data; name=\"/path/to/a/file.jpg\"; filename=\"/path/to/a/file.jpg\"");
    CkRest_AddHeader(rest,"Content-Type","image/jpeg");
    CkRest_SetMultipartBodyStream(rest,fileStream2);

    CkRest_putPartSelector(rest,"0");

    CkRest_AddHeader(rest,"Expect","100-continue");

    strResponseBody = CkRest_fullRequestMultipart(rest,"POST","/v1/files");
    if (CkRest_getLastMethodSuccess(rest) != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkStream_Dispose(fileStream2);
        return;
    }

    jsonResponse = CkJsonObject_Create();
    CkJsonObject_Load(jsonResponse,strResponseBody);

    id = CkJsonObject_stringOf(jsonResponse,"id");
    object = CkJsonObject_stringOf(jsonResponse,"object");
    created = CkJsonObject_IntOf(jsonResponse,"created");
    filename = CkJsonObject_stringOf(jsonResponse,"filename");
    purpose = CkJsonObject_stringOf(jsonResponse,"purpose");
    size = CkJsonObject_IntOf(jsonResponse,"size");
    type = CkJsonObject_stringOf(jsonResponse,"type");
    url = CkJsonObject_stringOf(jsonResponse,"url");


    CkRest_Dispose(rest);
    CkStream_Dispose(fileStream2);
    CkJsonObject_Dispose(jsonResponse);

    }

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