Skip to main content
1 of 5
b3m2a1
  • 47.1k
  • 3
  • 95
  • 245

URLFetch Multipart POST fails to post Body?

The service connection framework uses URLFetch to do its dirty-work and so I am stuck using that too.

Unfortunately for multipart uploads there's something bizarro going on. Consider the following:

url = First@
 CloudDeploy[
  Delayed[HTTPRequestData[{"Method", "Headers",
      "Body"}], "WL"]];

In[631]:= ToExpression@
 URLFetch[url,
  "MultipartElements" -> {{"metadata", "application/json"} -> {123, 
      10, 32, 32, 32, 32, 34, 110, 97, 109, 101, 34, 58, 32, 34, 116, 
      101, 115, 116, 46, 116, 120, 116, 34, 10, 125}, {"contentdata", 
      "application/vnd.wolfram.mathematica.package"} -> {116, 101, 
      115, 116, 32, 115, 116, 114, 105, 110, 103}},
  "Method" -> "POST",
  "CredentialsProvider" -> None
  ]

Out[631]= {"POST", {"host" -> "www.wolframcloud.com", 
  "user-agent" -> "Wolfram HTTPClient 11.1", "accept" -> "*/*", 
  "expect" -> "100-continue", 
  "content-type" -> 
   "multipart/form-data; \
boundary=------------------------7271f99252c452e1", 
  "x-forwarded-proto" -> "https", 
  "x-forwarded-for" -> "76.118.178.116, 10.11.32.157", 
  "x-forwarded-host" -> "www.wolframcloud.com", 
  "x-forwarded-server" -> "www.wolframcloud.com", 
  "content-length" -> "371"}, Missing["NotAvailable"]}

Notice that the "Body" element is missing. For some reason the "POST" call didn't the multipart-encoded stuff.

On the other hand:

In[632]:= ToExpression@
 URLFetch[url,
  "MultipartElements" -> {{"metadata", "application/json"} -> {123, 
      10, 32, 32, 32, 32, 34, 110, 97, 109, 101, 34, 58, 32, 34, 116, 
      101, 115, 116, 46, 116, 120, 116, 34, 10, 125}, {"contentdata", 
      "application/vnd.wolfram.mathematica.package"} -> {116, 101, 
      115, 116, 32, 115, 116, 114, 105, 110, 103}},
  "Method" -> "GET",
  "CredentialsProvider" -> None
  ]

Out[632]= {"GET", {"host" -> "www.wolframcloud.com", 
  "user-agent" -> "Wolfram HTTPClient 11.1", "accept" -> "*/*", 
  "expect" -> "100-continue", 
  "content-type" -> 
   "multipart/form-data; \
boundary=------------------------2e317ef0d18c6ab7", 
  "x-forwarded-proto" -> "https", 
  "x-forwarded-for" -> "76.118.178.116, 10.11.32.157", 
  "x-forwarded-host" -> "www.wolframcloud.com", 
  "x-forwarded-server" -> "www.wolframcloud.com", 
  "content-length" -> 
   "371"}, "--------------------------2e317ef0d18c6ab7
 
 Content-Disposition: form-data; name=\"metadata\"
 
 Content-Type: application/json
 
 
 
 {
     \"name\": \"test.txt\"
 }
 
 --------------------------2e317ef0d18c6ab7
 
 Content-Disposition: form-data; name=\"contentdata\"
 
 Content-Type: application/vnd.wolfram.mathematica.package
 
 
 
 test string
 
 --------------------------2e317ef0d18c6ab7--
 
 "}

That seems to work fine. So the "GET" request was okay but the "POST" one failed.

Is there a reason for this? Is there a way around it?

(Note that this is an for met issue even though the "MultipartElements" field of the HTTPRequestData encodes correctly. The Google Drive upload API endpoint notices an empty "Body" and throws an error.)

b3m2a1
  • 47.1k
  • 3
  • 95
  • 245