I'm playing with Salesforce in Mathematica trying to automate some PDF upload. I would like to know how can I execute the HTTPRequest as described by the curl below:

enter image description here

Here is the link for Salesforce website with copyable text.

How I do insert data where the text says: "Binary data goes here"? What is this HTTPRequest structure?

Any clue?

Crosspost in Wolfram Community link.


Caveat: I do not have a SalesForce account and have not tried this code against the real site. But it works against the testing site https://httpbin.org/anything.

For this example we will create minimal PDF content to be uploaded as the main document:

$pdf = ExportString[Image[{{1}}], "PDF"];

The SalesForce API requires us to create metadata for that document, expressed as JSON:

$entityDocument =
  <| "Description" -> "Marketing brochure for Q1 2011"
   , "Keywords" -> "marketing,sales,update"
   , "FolderId" -> "005D0000001GiU7"
   , "Name" -> "Marketing Brochure Q1"
   , "Type" -> "pdf"
   |> //
 ExportString[#, "JSON"]&;

We will also need our own SalesForce site and API token:

$site = "yourinstance.salesforce.com";    (* or "httpbin.org/anything" *)
$apiToken = "InsertRealTokenHere";

Now we can assemble an HTTP request that includes the metadata and file as separate parts. The form part named entity_document will specify the metadata whereas the part named Body will specify the document content.

$req = HTTPRequest[
  , <| "Method" -> "POST"
     , "Headers" -> <| "Authorization" -> "Bearer "~~$apiToken |>
     , "Body" -> <| "entity_document" ->
                     <| "MIMEType" -> "application/json"
                      , "Content" -> $entityDocument
                  , "Body" ->
                      <| "Name" -> "2011Q1MktgBrochure.pdf" 
                       , "MIMEType" -> "application/pdf"
                       , "Content" -> $pdf

The "Content" property of a form part could also take a value of ByteArray[...] for arbitrary binary data or of File[...] to reference a file already stored on disk.

Finally, we can submit the request using URLRead and extract properties of interest from the response:

$resp = URLRead[$req];


(* {"Body","BodyByteArray","BodyBytes","CharacterEncoding","ContentType","Headers","StatusCode","StatusCodeDescription","Version"} *)


(* 200 *)


(* application/json *)


    "id" : "015D0000000N3ZZIA0",
    "errors" : [ ],
    "success" : true
  • $\begingroup$ That's very cool. I'll try it with my Salesforce credentials and comment the result here. $\endgroup$
    – Murta
    Dec 30 '20 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.