Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

The Import command lets you do a HTTP GET from a URL, like this

Import @ "http://merkosoncampus.com/wp-content/uploads/2011/05/higgs-boson.jpg"

This sends an HTTP GET request to the server and presumably parses the response according to some rules for inferring the type of data, from the content or from the extension ".jpg" or whatever.

Listening on the socket, we see something like this

GET http://merkosoncampus.com/wp-content/uploads/2011/05/higgs-boson.jpg HTTP/1.1
User-agent: Mathematica/8.0.4.0.0 PM/1.3.1
Host: merkosoncampus.com
Proxy-Connection: Keep-Alive

whereas what I want is something along the lines of

POST http://foo.bar.com/myService HTTP/1.1
User-Agent: Mathematica/8.0.4.0.0 PM/1.3.1
Host: foo.bar.com
Accept: */*
Connection: Keep-Alive
Content-Length: 76
Content-Type: application/x-www-form-urlencoded

{ "myJsonStuff" : {"number" : 1} }

I looked for options in Import, praying for something like Method->"POST", but didn't find anything useful.

I'd be grateful for clues, experience, ideas, sample code. In the mean time, I intend to work around it by writing my desired JSON to a file and using curl to POST it, but it's way sub-optimal.

share|improve this question
3  
See for example stackoverflow.com/questions/5853134/… –  belisarius Mar 16 '12 at 19:49
1  
For a real example of how POST requests can be done, see this: mathematica.stackexchange.com/questions/30748/… –  Pickett Sep 17 '13 at 8:14
add comment

3 Answers 3

In a post about automated image uploading Arnoud Buzing describes an undocumented and unsupported POST method. It looks like this:

 xml = Import[url, "XML", "RequestMethod" -> "POST", 
              "RequestParameters" -> {"key" -> key, "image" -> image}];
share|improve this answer
    
This is a step in the right direction; I think I discovered that "RequestBody" and "RequestBodyString" are both available options, but I am having quite a struggle with Fiddler and with the Apache Jakarta Java documentation. I'll get back to this next week; doesn't seem trivial. –  Reb.Cabin Mar 17 '12 at 1:14
2  
@Reb.Cabin If you manage to discover more options, please do share them in an answer. I have not been able to make multi part POST requests work with Import. –  Szabolcs Mar 17 '12 at 9:25
    
Szabolcs will do. I have an idea on the fiddler problem ( a day of rest often brings fresh insights ). I may just write a new java class to do it. More later. –  Reb.Cabin Mar 18 '12 at 2:11
    
Just remembered this one, which has the necessary java juju meta.mathematica.stackexchange.com/questions/5/… –  Reb.Cabin Mar 18 '12 at 2:34
add comment
up vote 13 down vote accepted

Using the great example of Szabolcs from here, I implemented the following little function that solves my problem. You must give it a List of Rules, and it will send them to the hard-coded web-service endpoint inside. To test it, I implemented a little NodeJs server that reads the JSON body of a POST request, parses it, unparses it, and sends it back. So this is just a very expensive way of converting rules into JSON in MMA, checking their syntax in Node.Js, and sending them back to MMA to be decoded back into native rules.

bounceRules[requestRules_] :=
 Module[{
   requestJsonString,
   url, client, method,
   entity, responseCode, response,
   responseRules, responseExpression},
  JavaBlock[
   requestJsonString = ExportString[requestRules, "JSON"];
   url = "http://127.0.0.1:5000/bounce";
   client = JavaNew[
     "org.apache.commons.httpclient.HttpClient"];
   method = JavaNew[
     "org.apache.commons.httpclient.methods.PostMethod", url];
   entity = JavaNew[
     "org.apache.commons.httpclient.methods.StringRequestEntity",
     jsonString];
   method@setRequestEntity[entity];
   responseCode = client@executeMethod[method];
   If[responseCode === 200,
    (* then *)
    response = method@getResponseBodyAsString[];
    responseRules = ImportString[response, "JSON"];
    (* else *)
    Message[
     bounceRules::httperr,
     responseCode];
    $Failed
    ]]]

My main hangup, btw, was due to the fact that http://localhost inexplicably does not work. I get timeouts and exceptions and all manner of opaque torture from the java stack. Once I switched to http://127.0.0.1 everything just worked.

share|improve this answer
    
Unfortunately I don't know any Java, and it was really difficult for me to get that working ... My sources were this and this, both by ragfield, and also Arnoud's replies –  Szabolcs Mar 18 '12 at 17:06
add comment

Use URLFetch in Mathematica 9.0.1.

share|improve this answer
1  
Can you elaborate a bit on this and perhaps give a short example? –  Szabolcs Apr 16 '13 at 15:47
8  
Here's an example: URLFetch["http://foo.bar.com/myService", "Method"->"POST", "BodyData"->"{ \"myJsonStuff\" : {\"number\" : 1} }"] –  Todd Gayley Apr 16 '13 at 17:55
    
Thanks Todd :-) –  jamtype7 Apr 20 '13 at 8:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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