Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



Writing the API: A Simple POST

In this part of the tutorial, you'll add support for a simple POST to the backend API that you created previously. The incoming request contains a user-supplied message and integer; the method repeats the message the number of times specified by the integer and returns the results.

Adding a POST method

  1. In the helloworld_api.py file created previously, add the following lines above the line ID_RESOURCE = endpoints.ResourceContainer :

    MULTIPLY_METHOD_RESOURCE = endpoints.ResourceContainer(
            Greeting,
            times=messages.IntegerField(2, variant=messages.Variant.INT32,
                                        required=True))
    
    @endpoints.method(MULTIPLY_METHOD_RESOURCE, Greeting,
                      path='hellogreeting/{times}', http_method='POST',
                      name='greetings.multiply')
    def greetings_multiply(self, request):
        return Greeting(message=request.message * request.times)
    
  2. When you are finished, your file should look like this:

    """Hello World API implemented using Google Cloud Endpoints.
    
    Defined here are the ProtoRPC messages needed to define Schemas for methods
    as well as those methods defined in an API.
    """
    
    
    import endpoints
    from protorpc import messages
    from protorpc import message_types
    from protorpc import remote
    
    package = 'Hello'
    
    
    class Greeting(messages.Message):
        """Greeting that stores a message."""
        message = messages.StringField(1)
    
    
    class GreetingCollection(messages.Message):
        """Collection of Greetings."""
        items = messages.MessageField(Greeting, 1, repeated=True)
    
    
    STORED_GREETINGS = GreetingCollection(items=[
        Greeting(message='hello world!'),
        Greeting(message='goodbye world!'),
    ])
    
    
    @endpoints.api(name='helloworld', version='v1')
    class HelloWorldApi(remote.Service):
        """Helloworld API v1."""
    
        @endpoints.method(message_types.VoidMessage, GreetingCollection,
                          path='hellogreeting', http_method='GET',
                          name='greetings.listGreeting')
        def greetings_list(self, unused_request):
            return STORED_GREETINGS
    
        MULTIPLY_METHOD_RESOURCE = endpoints.ResourceContainer(
                Greeting,
                times=messages.IntegerField(2, variant=messages.Variant.INT32,
                                            required=True))
    
        @endpoints.method(MULTIPLY_METHOD_RESOURCE, Greeting,
                          path='hellogreeting/{times}', http_method='POST',
                          name='greetings.multiply')
        def greetings_multiply(self, request):
            return Greeting(message=request.message * request.times)
    
        ID_RESOURCE = endpoints.ResourceContainer(
                message_types.VoidMessage,
                id=messages.IntegerField(1, variant=messages.Variant.INT32))
    
        @endpoints.method(ID_RESOURCE, Greeting,
                          path='hellogreeting/{id}', http_method='GET',
                          name='greetings.getGreeting')
        def greeting_get(self, request):
            try:
                return STORED_GREETINGS.items[request.id]
            except (IndexError, TypeError):
                raise endpoints.NotFoundException('Greeting %s not found.' %
                                                  (request.id,))
    
    
    APPLICATION = endpoints.api_server([HelloWorldApi])
    

Running and Testing

To run and test your new additions to the backend API:

  1. Run the backend you just created by invoking the command

    ~/path/to/python/sdk/google_appengine/dev_appserver.py  helloworld
    

    When the backend is running successfully, a message similar to this one is displayed:

    INFO     2013-10-07 19:41:16,687 admin_server.py:117] Starting admin server at: http://localhost:8000
    
  2. In your browser, visit this URL:

    http://localhost:8080/_ah/api/explorer
    
  3. In the API Explorer, after you click helloworld API , notice that the list now also contains the new method. Click helloworld.greetings.multiply to display the Explorer form for it:

    greetings.multiply

  4. In the form's times textbox, supply the number of times you want the POSTed message to be echoed in the response, for example, 3 .

  5. Click inside the Request body control, click add a property > message , add the text message "Charles Darnay ", then click Execute .

In the Response, you'll see the success message and the message " Charles Darnay Charles Darnay Charles Darnay ".

Code summary

The greetings.multiply method supporting POST uses a ResourceContainer instead of a simple message class type, because the method must support requests containing an argument in the URL path.

Next...

Next, we'll add a method that is access-restricted by OAuth 2.0.

Continue to Adding a Method Protected by OAuth 2.0 .

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.