1

I am using Flask to create an application that receives some data from a server with an HTTPS POST and process its data. Once i receive the message from the server I need to send a "status = 200" so the server will know that i got the info, and stop re-sending me the information.

Do i have to send a HTTPS GET? It is not really a get as the only thing i need is to notify the server i got the message...

How can i do it in python? I need to do it after processing the data i receive but without return.

app = Flask(__name__)
@app.route('/webhook', methods=['POST','GET'])``
def webhook():


req = request.form
xml = req['data']
#HOW DO I SEND A CODE 200 NOW???
info = ET.fromstring(xml)
print info[0].text
print info[1].text
print info[2].text
print info[3].text

Thanks!

Alex
  • 11
  • 1
  • 1
  • 3

1 Answers1

5

If you want to be sure about the response code, you can send an empty response and only specify the response code like so

return '', 200

The complete answer can be found at Return HTTP status code 201 in flask

UPDATE FOR UPDATED QUESTION:

You could use a Thread for that.

import threading

app = Flask(__name__)
@app.route('/webhook', methods=['POST','GET'])``

def worker(xml):
  info = ET.fromstring(xml)

  print info[0].text
  print info[1].text
  print info[2].text
  print info[3].text

  return

def webhook():
  req = request.form
  xml = req['data']

  # This executes in background
  t = threading.Thread(target=worker, args=(xml,))
  t.start()

  # So that you can return before worker is done
  return '', 200
finngu
  • 457
  • 4
  • 23
  • The thing is that actually I need to return something once i have process the data. Can i send the status 200 just after reciving the data in the middle of the code? I have just update the question to show it – Alex Jun 04 '17 at 11:30
  • @Alex once you return a response - that's it... (unless you're passing off to a worker process/thread/whatever) However, we're now approaching a completely new question. "How do I return a 200 response" has been answered. – Jon Clements Jun 04 '17 at 12:25
  • Yes, you are right. But do you have any ideas about how to solve the new question? – Alex Jun 04 '17 at 15:11