Writing the API: A Simple HTTP 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 HTTP POST method

The GreetingApi service has method called multiply that receives a request containing a message and the number of times to multiply that message.

# This ResourceContainer is similar to the one used for get_greeting, but
# this one also contains a request body in the form of a Greeting message.
MULTIPLY_RESOURCE = endpoints.ResourceContainer(
    times=messages.IntegerField(2, variant=messages.Variant.INT32,

    # This method accepts a request body containing a Greeting message
    # and a URL parameter specifying how many times to multiply the
    # message.
    # This method returns a Greeting message.
def multiply_greeting(self, request):
    return Greeting(message=request.message * request.times)

Running and Testing

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

  1. Start a new Chrome session as described in How do I use Explorer with a local HTTP API and specify 8080 as the localhost port (localhost:8080). Alternatively, you can use Firefox and temporary disable mixed content blocking.

  2. In the project directory, start the development server:

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

    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
  3. In your browser, visit this URL:


    This opens up the API Explorer for your backend. Notice the list of APIs displayed with Greeting API in that list.

  4. In the API Explorer, after you click greeting API, notice that the list now also contains the new method. Click greeting.greetings.multiply to display the Explorer form for it:


  5. In the form's times textbox, supply the number of times you want the message that was sent as an HTTP POST request to be echoed in the response, for example, 3.

  6. 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 that supports HTTP POST requests uses a ResourceContainer instead of a simple message class type, because the method must support requests containing an argument in the URL path.

What's next

Next, you will add a method that is access-restricted by OAuth 2.0.

Continue to Adding a Method Protected by OAuth 2.0.

Send feedback about...

App Engine standard environment for Python