views:

3352

answers:

3

I am trying to get a Flex application to communicate with a custom python webserver I have developed.

I am noticing that I cannot read the postdata received because Flex does not seem to include the Content-Length in the HTTP headers. (My webserver work when posted to from plain HTML)

Is this a known problem? any ideas how to set the content-length header?

Here is the current headers being sent:

Host: localhost:7070

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0

.3

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 300

Connection: keep-alive
A: 

I don't believe this is a known problem.

Are you sure no Content-Length is being sent? You've posted the request side of the HTTP interaction, coming from your browser; there is never a Content-Length header on that side of the protocol.

A. Rex
+2  A: 

It should, so long as you set your HTTPService's method property to POST. If you omit it, it will default to GET, and the parameters will be sent as part of the query string, not as POST data.

I set up this scenario using this Flex code:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application  layout="absolute"
    xmlns:mx="http://www.adobe.com/2006/mxml"
    creationComplete="init()">

    <mx:HTTPService id="service" 
     url="http://localhost:8000/"
     method="POST"
     resultFormat="text"
     result="response.htmlText=ResultEvent(event).result.toString()"/>

    <mx:Text id="response" width="100%" height="100%"/>

    <mx:Script>
     <![CDATA[
      import mx.rpc.events.ResultEvent;
      private function init() : void {
       service.send({
        foo: "Fred",
        bar: "Barney"
       });
      }
     ]]>
    </mx:Script>
</mx:Application>

And this python server code:

#!/usr/bin/env python

import SimpleHTTPServer, BaseHTTPServer, string

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_POST(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write("<html><body>")
        self.wfile.write("<b>METHOD:</b> " + self.command)

        # Write out Headers
        header_keys = self.headers.dict.keys()
        for key in header_keys:
            self.wfile.write("<br><b>" + key + "</b>: ")
            self.wfile.write(self.headers.dict[key])

        # Write out any POST data
        if self.headers.dict.has_key("content-length"):
            content_length = string.atoi(self.headers.dict["content-length"])
            raw_post_data = self.rfile.read(content_length) 
            self.wfile.write("<br><b>Post Data:</b> " + raw_post_data) 
        self.wfile.write("</body></html>")
    def do_GET(self):
        self.do_POST()

try:
    BaseHTTPServer.test(MyHandler, BaseHTTPServer.HTTPServer)
except KeyboardInterrupt:
    print 'Exiting...'

And got this result:

METHOD: POST
content-length: 19
accept-language: en-us,en;q=0.5
accept-encoding: gzip,deflate
connection: keep-alive
keep-alive: 300
accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
user-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1
accept-charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
host: 10.0.7.61:8000
content-type: application/x-www-form-urlencoded
Post Data: bar=Barney&foo=Fred

So it should work.

bill d
A: 

As Bill D says, you almost certainly are not doing a POST, as we do those all the time, fielding them with our server code and it most certainly includes the Content-Length.

Jonathan