Why your HTTPService “GET” changes to “POST” while you’re not looking

I’m trying to get my AIR application to communicate with Basecamp. Should be easy. The guys at 37Signals have their signature REST simplicity going on and the API is just sitting there waiting for basic POST and GET requests via AIR’s HTTPService.

Ok, so I want to get my projects from the Basecamp API. Lessee here…reading through the Basecamp API (and translating to ActionScript) I just need to send an HTTP “GET” to my account’s URL, making sure to add on a “/projects” to the URL…something like https://myAccountName.basecamphq.com/projects

And, I also need to make sure that the headers include stuff like contentType=”application/xml” and I set the “Accept” property on the headers object to “application/xml” too. No prob.

And…AHA!….I need to take a little time to get the authentication header correct, as described by Alain Hufkins in this blog post.

Finally, as I mentioned before, if you want a simple list of your items (in this case projects), the convention in REST is to issue a GET request. So we’ll add this to our HTTPService’s settings as well.

Now, with just a little pompousness (soon to evaporate) we’ve got this thing down in just a few minutes. Here’s the code in my BasecampDelegate class, kind of squished together in one method so you can see it. I’m using the SWIZ framework, so the HTTPService is injected into the class’s “service” property, but you could just create the service within the method and everything would go. I also have a basecampModel class that contains the URL, username and password.

public function getProjects():AsyncToken
{
     Logger.debug(" what could be easier? ...")
     service.url = basecampModel.basecampURL + "/projects"
     service.method = "GET"
     var username:String = basecampModel.basecampLogin
     var password:String = basecampModel.basecampPassword
     var enc:Base64Encoder = new Base64Encoder();
     enc.encode(username + ":" + password);
     service.headers["Authorization"] = "Basic " + enc.toString();
     service.contentType="application/xml"
     service.headers["Accept"] = "application/xml"
     service.resultFormat = "e4x"
     return service.send()
}

Ok! (Rubbing hands gleefully). Click debug/compile and …. wait…. Flex is tracing out that the request is being sent via “POST.” But I explicitly set it to “GET.” Try re-arranging how the parameters are set. Debug/compile … same thing. Arrrrrr!!! Ok, make sure the original tag is set to “GET”. Debug/compile … same thing.

“What the…”

It turns out that the HTTPService class changes stuff around on you when you’re not looking : if you set your contentType to “application/xml”, it doesn’t matter how many times you set it to “GET,” it’s going to send off a “POST” at runtime. Big thanks to Verveguy for pointing this out.

So, the way around this is to set your content type to x-www-form-urlencoded even though the Basecamp API docs tell you you need to send application/xml. This will work when you’re doing a GET.

  service.contentType="application/x-www-form-urlencoded"

I hope this saves you some time!

Explore posts in the same categories: AIR

One Comment on “Why your HTTPService “GET” changes to “POST” while you’re not looking”

  1. Seth Says:

    HUGE.

    I’m building an air app aswell and ran into this issue. Flex was even tracing that it was a GET, but Charles was saying it was a POST. This

    i spent half day trying to figure out why my service wasn’t working.

    thnx.

Comment: