So far all the user scripts that have been created rely on the fact that a URL can receive parameters as part of the URL. This is referred to as GET. There are some web pages that do not encode the data on the page in the URL, they encode it as part of an overall message. This is referred to as POST. This page will explain how to do POST requests in an Operator action. Note this page assumes you have already created an action for Operator.
Because the POST data is hidden from you, we need to figure it out. To do this, we’re going to use the Live HTTP Headers extension. This extension will allow us to see exactly what is going on when the web page sends data so we can copy it. For our demo, we’re going to create an extension that finds the nearest Dave Ramsey’s Financial Peace University class. Note that as soon as you go to this page, you’ll see the form we’re going to emulate. Let’s fire up Live HTTP Headers and see what we’ve got. Fill in a zip code and click Submit.
Live HTTP Headers will show a lot of requests, but we’re interested in the one at the top. In particular, we’re looking for this:
This is the important information about the transaction. We can see that this service POSTs the zip code, the distance and two other values. So our job is going to be to emulate that request.
First let’s take a look at the completed action since most of what we are doing is boilerplate. The completed action is here.
You’ll notice that the really important part of the action is setting up the body. We’ll use the data we have in an adr microformat to create the body:
var body = "intZipCode=" + semanticObject["postal-code"] + "&intMaxRad=30";
Note that we’ve left off the two extra parameters since they seem to have no effect on what the page displays.
The other things that will need to be changed in the boilerplate are URLS. There are two important URLs that we can get from Live HTTP Headers. The first is the actual POST url. Up at the top, you see:
Putting the server on the front gives us the actual POST url.
The other important URL is the Referer. This URL is important because some POSTs don’t work correctly unless they come from a certain URL. You’ll see the Referrer URL is:
We’ll want to put both these URLs in the appropriate place in our boiler plate.
And that’s pretty much it. You can modify the existing action I’ve given you to POST to any web page by using Live HTTP Headers to determine the information you need and modifying the body variable and the URLS.
Note there might be cases where you want to use Operator send an actual file. For that, I’ll point you to an example that shows how to use the Dopplr service to upload a vCard to their service. It’s similar in some ways, but there’s a little more complexity in setting up the request. Here’s that action.