Message Boards Message Boards

1
|
21357 Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

How do you send a image file via HTTP POST? Using URLExecute

I am trying to send an image file via HTTP Post. I can do this in Java, but I can't seem to get it working with URLExecute. I have further details on Mathematica StackExchange (bounty of 100). The only answer so far seems to suggest that this is a limitation of URLExecute and can't be done?

If it isn't possible where do you log a bug/enhancement request?

Copy of MSE post

I have to submit a form like as follows:

<input type="file" name="jpgFile">

I can't seem to find any instructions on sending this in URLExecute. If I specify the file name as the parameter it fails. If I try Import[filename, "JPG"] of the image it also fails as invalid.

I thought his might work, but I get an error "Iteration limit of 4096 exceeded". This suggests it might be working as test file is 451,107 bytes.

URLExecute[ url,
 "Method" -> "POST",
 "Headers" -> {
   "Accept" -> "application/json; charset=UTF-8"
   },
 "MultipartElements" -> { 
   {"jpgFile", "file"} -> "test.jpg"
  }
 ]

So this is another example. In this instance I am sending a text file and this appears to work.

testData = Import["test.txt","Text"];

URLExecute[
 url,
 "Method" -> "POST",
 "Headers" -> {
   "Accept" -> "application/json; charset=UTF-8"
   },
 "MultipartElements" -> { 
   {"textData", "text/plain"} -> testData
   }
 ]

From a Java point of view, the following actually works: http://stackoverflow.com/a/17174260/1167890

I would like not to be forced back into Java/MathLink if possible though.

POSTED BY: Simon O'Doherty
2 Replies

Thanks, but that didn't work. The first solution in that page is an embedded image into a HTML tag. Which won't work for me.

The second part seemed to work, but the server came back with "Cannot upload file". So I suspect it is not formatting it correctly.

Good news though! Someone did answer my question. As I understand it, it's a hack, but works.

http://mathematica.stackexchange.com/a/97777/16072


Copy of MSE post

As pointed out yesterday by ZachB in answer to another question, there is a hack that makes URLFetch add a filename to the Content-Disposition header as required by RFC 7578 multipart/form-data section 4.2:

URLFetch[url,
"Method"->"POST",
"MultipartElements"->{
    {"file\"; filename=\"test.jpg","image/jpeg"}->
        Import["test.jpg", "String"]
 }
]

Here, the Multipart element name is "file" but URLFetch is tricked into adding the filename "test.jpg" by hiding it within a longer element name.


POSTED BY: Simon O'Doherty

Simon, have a look at:

Simple inexpensive delivery service outperforms SendMail

Christopher has solved this i think for the mailgun api. See his last post for details about multipart support.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract