HTTPServiceInvoker

(This tag must be placed inside an <EventHandlers> tag or a <MessageHandlers> tag)

The HTTPServiceInvoker tag is used to create an HTTP Service instance and make a GET or POST request to that service. To use this tag, you need to specify the same attributes you would when creating an HTTP service with the <mx:HTTPService> tag (with the exception of xmlDecode, xmlEncode, and lastResult). In addition, you need to specify what method to call. This tag will also accept all mx.rpc.http.HTTPService tag attributes.

<HTTPServiceInvoker url="URLToCall" />

The above example would be the same as doing the following:

<mx:HTTPService id="myServiceInstance" url="http://www.example.com/services">

myServiceInstance.send();

You would also need to either create event handlers for the server result and fault or create a Responder to handle them. That is not necessary when using the HTTPServiceInvoker tag. See section "Handling a service result or fault".

With this tag, you can also utilize an already created HTTPService instance. This is useful when the same services are used by several EventHandlers blocks or several EventMaps.

Suppose you have a HTTP Service tag (either in the event map itself or in a different file):

<mx:HTTPService id="myService" url="http://www.example.com/services" />

You can call a method on that already created service as follows:

<HTTPServiceInvoker instance="{myService}" />

Attributes

url

required if no instance is specified

This attribute specify the address to send the request. Refer to mx.rpc.http.HTTPService documentation for more information.

instance

An HTTPService instance

The instance attribute specifies the already created service instance to use to make the call. This attribute must be supplied using bindings because it needs to point to an already created object.

Suppose you have a HTTPService already created:

<mx:HTTPService id="myService" url="http://www.example.com/services" />

You can make a call to this service by using the HTTPServiceInvoker tag, with instance {myService}. Note that any property you need for your service must be defined in the HTTPService tag.

<HTTPServiceInvoker instance="{myService}" />

debug

Boolean

Whether to show debugging information for this HTTPService resultHandlers and faultHandlers. If true, Console output will show debugging information as those handlers run.

Inner tags

Request

You can add variables to your HTTP request by using the Request inner tag. If using GET method, these variables will be sent as query string parameters. Otherwise, they will be added to the POST request.

As attributes of the Request tag, you specify the names of the variables you want to send and set the values of those variables by settings the value of the attributes.

Suppose you need to send a "photo_id" variable and a username variable with your request. Then you will specify:

<HTTPServiceInvoker instance="{myService}">
	<Request 
		photo_id="17"
		username="{event.username}" />
</HTTPServiceInvoker>

resultHandlers

A set of handlers to run when the server call returns a result. Inside this inner tag, you can use the same tags you would in the main body of an <EventHandlers> block, including other service calls.

faultHandlers

A set of handlers to run when the server call returns a fault. Inside this inner tag, you can use the same tags you would in the main body of an <EventHandlers>, including other service calls.

Handling a service result or fault

You can have a resultHandlers block and a faultHandlers block inside the tag to handle service results and faults.

<HTTPServiceInvoker url="URLToCall">

	<resultHandlers>
		... this list executes when server returns results ...
	</resultHandlers>
	
	<faultHandlers>
		... this list executes when server returns an error ...
	</faultHandlers>
	
</HTTPServiceInvoker>

See "Handling a service result or fault" for more information.

17 responses

  1. Is there a way to supply a hash to the Request tag? For example, with traditional mx:request, I can have:

    <mx:request>
    <person>
    <name> Joe </name>
    <age> 29 </age>
    </person>
    </mx:request>
  2. Or conversely, I could have httpSvc.send({person: {name: Joe} {age: 29}})
  3. Hi Justin,
    The HTTPServiceInvoker tag has a request attribute that works the same way as the <mx:request> as you showed in your first comment. I haven't tried it, but I think you should also be able to say:
    <code>
    <HTTPServiceInvoker request="{person: {name: Joe} {age: 29}}" ...>
    </code>

    Both ways would only work for literal values, however, but you would not be able to access what we call "Smart Objects", such as event, result, lastReturn. So you won't be able to supply event.age as a replacement for the value "29".

    To get around that, you could create your object before you pass it to the request using ObjectBuilder:

    <code>
    <ObjectBuilder generator="{Object}" cache="false">
       <Properties name="{event.name}" age="{event.age}" />
    </ObjectBuilder>

    <HTTPServiceInvoker>
        <Request person="{lastReturn}" />
    </HTTPServiceInvoker >
    </code>

    If you need more "nesting", then you could have multiple ObjectBuilder tags, but to be honest, it would be messy. I would suggest you to use another class to do that, so you would use <MethodInvoker> to call a function "createMyPerson(name, age)" or similar, and then pass that with lastReturn to the Request tag. That would make it nicer if your object has many levels of nested properties.
  4. Laura,

    Thanks for the response; the information about the ObjectBuilder was very helpful. As it is, I ended up nesting the data within the event itself (converting the public property to a getter/setter with the business logic I needed) and that worked perfectly. I thought that it probably makes better sense to have the logic in the custom creation event than cluttering the EventMap.
  5. First, great framework so far, but I still have to work out some techniques for bigger applications. I hope you have some recommendations or can add some stuff to the best practice section.

    I want to set some Request Parameters depending from a kind of Authorization Manager like you used in the cafeTownsend example.
    <HTTPServiceInvoker ...
    <Request username="{AuthorizationManager.user.name}" />

    However, I cannot access the manager from this scope, without using the ObjectBuilder in every HTTPServiceInvoker. I will have loads of them.

    Can you give a recommendation?

    The other problem is to create a Manager Object and setting values given via flashVars. I cannot access the Application from inside the EventMap.

    Thanks in advance!
    Simon
  6. I've try to use <mx request> tag inside the HTTPServiceInvoker tag but it cause an error to the compiler.I changed it to <mate request> but the problem still exist.How can i use the <mx request> tag in HTTPServiceInvoker as i use it in HTTPService ?

    My xml is like :
    <content name="/MainPageTemplate">
    <page name="Mobile Marketing Platform Keyword Manager" />
    <content localname="Nav" name="/TwoRowNav">
    <content localname="MSMNav" name="/:MSMNav">
    <content localname="MSMNav" name="/MSMNav">
    <Client>
    <clientId>2</clientId>
    <name>Velti Tech</name>
    <Product>
    <productId>6</productId>
    <clientId>2</clientId>
    <name>V-Phone</name>
    </Product>
    </Client>
    <Client>
    <clientId>4</clientId>
    <name>General Motors</name>
    <Product>
    <productId>8</productId>
    <clientId>4</clientId>
    <name>Saturn Vue</name>
    </Product>
    </Client>
    </content>
    </content>
    </content>
    </content>

    and i want to use clientId and productId

    Thanks

    Aris
  7. How would I go about specifying a variable URL for the request? I need to pass an id to the server script as part of the URL. Thanks in advance!
  8. Panos,
    Do you want to send that whole xml as the request?

    franz,
    You need to use the Request inner tag. If the variable happens to be "id", the you need to use
    <Request _id="YOUR ID" /> (note the underscore).
  9. I only want to past productId and cientId.I have done it with tag
    <Request />

    Thanks for the answer


    Panos
  10. <HTTPServiceInvoker id="httpService" url..."
    resultFormat="e4x" debug="true">
    <Request             sqlName="listTrackByLayout"             layout_sa="{event.layoutSa}"
       trk_type ="{event.trkType}" />
    <resultHandlers> ... </resultHandlers>
    <faultHandlers> ... </faultHandlers>
    </HTTPServiceInvoker>

    If I have multiple request parameter set by event object , my back end servlet can only get one parameter passed by event. that is layout_sa.
    and trk_type parameter cannot be catched by back end.
    if I used explicit parameter ,layout_sa="1", then I can get another event passed parameter trk_type.

    What should I do in this case ?
  11. I've used
    <Request
    _id="{event.myId}"
    rmk="{event.rmk}" />
    to send the parameter "id" to server side, but what the server get is:
    &id=event%2EmyId&rmk=testing

    Seems it directly print out the name "event.myId" but not the value of it.

    Could anyone please help? Thanks!
  12. Does the method default to POST?
  13. Ankur,
    I believe HTTPService (the normal Flex HTTPService) defaults to GET, so I think that is the default for the HTTPServiceInvoker tag.
  14. I have been trying to use HttpServiceInvoker to fetch data from web service using following code:

    <!-- UserEvent.FETCH_COMMENTS________________________________________________________________________ -->
       <mate:EventHandlers type="{UserEvent.FETCH_COMMENTS}" >
          <mate:MethodInvoker generator="{Config}" method="getappServicesURL" cache="global"/>
          <mate:MethodInvoker generator="{UrlConstructor}" method="fetchURL" arguments="{[event,lastReturn,event.userid]}" />
          <mate:HTTPServiceInvoker useProxy="false" url="{lastReturn}" method="GET" resultFormat="e4x" >
             <mate:resultHandlers>
                <mate:MethodInvoker generator="{Helper}" method="parseComments" arguments="{resultObject}" cache="none" />            
                <mate:MethodInvoker generator="{ModelManager}" method="setCommentList" arguments="{lastReturn}"/>
             </mate:resultHandlers>
          </mate:HTTPServiceInvoker>
          
       </mate:EventHandlers>
    <!-- UserEvent.POST_COMMENT ________________________________________________________________________ -->
       <mate:EventHandlers type="{UserEvent.POST_COMMENT}">
          <mate:MethodInvoker generator="{Config}" method="getappServicesURL" cache="global"/>
          <mate:MethodInvoker generator="{UrlConstructor}" method="fetchURL" arguments="{[event,lastReturn]}" />
          
          <mate:HTTPServiceInvoker useProxy="false" request="{event.xmlPayLoad}" url="{lastReturn}" method="POST" resultFormat="e4x" contentType="application/xml" >
             <mate:resultHandlers>
                <mate:EventAnnouncer generator="{UserEvent}" type="{UserEvent.FETCH_COMMENTS}">
                <mate:Properties userid="{event.userid}"/>
                   
             </mate:EventAnnouncer>
          </mate:resultHandlers>
          
          </mate:HTTPServiceInvoker>   
       </mate:EventHandlers>

    Hoewever, I noticed that the {resultObject} in Fetch_Comments event does not change teven after raising the event multiple times. Any idea what is wrong in the code ?
  15. How to pass a parameter value from event to the url mentioned below.

    <mx:HTTPService id="myService" url="http://www.example.com/services"; />

    <EventHandlers type="{TestEevent.TEST}" debug="true">
          <MethodInvoker id = "homeServices" generator="{HomeServices}" method="setUserId" arguments="{[event.userId]}" cache="none"/>
          <HTTPServiceInvoker instance="{services.getUserService(event.userId)}" debug="true">

    ...

    I am using like this. Here instead of setting the event.userId parameter value its setting "event.userId" as a string .. how to resolve this.
  16. I am still getting to know my way around Mate, so I may be overlooking something obvious. Here's what i want to accomplish:

    I am interested in encapsulating the resultHandler and faultHandler inside a service class. For example, I have a custom HTTPServcice.mxml file with the appropriate <s:HTTPService> tag and properties. I also have added internal methods to handle both success and fault results. I invoke the service from the EventMap using <InlineInvoker> :

    <EventHandlers type="arguments="GET_SMART_SUGGEST_KEYWORDS" />
    <InlineInvoker method="{httpService.getSmartSuggestData}" arguments="{event.data}"/>
    </EventHandlers>

    This is working as expected however, I want to dispatch an event back to the EventMap after the resuls are returned and reworked for the desired data structure which is handled inside the HTTPService.mxml. The EventMap is not responding to the event dispatched.

    I have tried several approaches with out any luck. FYI, I am passing the scope.dispatcher to the Service on instantiation like so:

    <services:HTTPService id="httpService" dispatcher="{scope.dispatcher}" />

    Any help will be appreciated. Thanks in advance.
  17. Ok, I stumbled on a solution. Passing scope.dispatcher in the arguments array on <InlineInvoker> solves my issue.

    <InlineInvoker method="{httpService.getSmartSuggestData}" arguments="{[event.data, scope.dispatcher]}"/>

    Not sure if this is ultimately correct so any recommendations for an alternate approach are welcome

Comments now closed