1

I am very new to app engine and python too so this might sound like a very basic question. I want to create a RESTful service which handles POST requests(using Python, app engine)

For Example:

www.myproject.appspot.com - is my URL

and if I do a simple GET call to this(from a browser or REST client etc), it returns what ever is in the code, like here it is >Hello!<

class MainHandler(webapp2.RequestHandler):
def get(self):
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.write("Hello!")

What I want to do is make it a POST request, like if I hit it with some JSON like

 {"myName" : NameString}

It will print the name in NameString. I know this sounds like a very silly question but please bear with me as my internet search has left me confused with which method to use some have suggested using EndPoints API, Django etc. But I believe my requirement is very basic and webapp2 can handle it.

I just want so direction or basic examples to do this.

Thanks!

2 Answers 2

1

Here's the way to get the POST data from the request into the response:

class MainHandler(webapp2.RequestHandler):
    def post(self):
        name = self.request.POST['myName']
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write("Hello, %s!" % name)
Sign up to request clarification or add additional context in comments.

1 Comment

Hey Thanks! but i had to tweak this code a bit to get what I wanted.
1

Write a method that will handle the POST method and set proper content type:

https://webapp-improved.appspot.com/guide/handlers.html#http-methods-translated-to-class-methods

In your case it would be:

import json
class MainHandler(webapp2.RequestHandler):
  def post(self):
    name = 'John Snow'
    self.response.headers['Content-Type'] = 'application/json'
    self.response.write(json.dumps({"myName" : name}))

1 Comment

Hey GOT fan..thanks, I had something different in mind but the link u have given is very useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.