3
$\begingroup$

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.

$\endgroup$
6

1 Answer 1

2
$\begingroup$

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[
  "https://"~~$site~~"/services/data/v50.0/sobjects/Document/"
  , <| "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];

$resp["Properties"]

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

$resp["StatusCode"]

(* 200 *)

$resp["ContentType"]

(* application/json *)

$resp["Body"]

(*
  {
    "id" : "015D0000000N3ZZIA0",
    "errors" : [ ],
    "success" : true
  }
*)
$\endgroup$
1
  • $\begingroup$ That's very cool. I'll try it with my Salesforce credentials and comment the result here. $\endgroup$ Commented Dec 30, 2020 at 18:15

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.