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
}
*)
HTTPRequesthas a new way to handle Multipart. Can be checked in URLRead Doc in the Scope Area $\endgroup$