title: making a http request within a api endpoint
body: <p>I'm trying to make a request within a post api endpoint. So far i've made it <code>Async</code>, however i'm wondering how the behaviour is in this case. if the <code>fetch</code> fails, i would like to output a message. However since it is <code>Async</code>n i guess that <code>self.finish</code> in the bottom will be outputted before <code>_upload_to_nsq()</code> is finished? Is the best here to make it synchronous or how can i improve the code to output messages during errors?</p>
  
  <pre><code>class Handler(base.BaseHandler):
  
      def _upload_to_nsq(self):
          request = tornado.httpclient.HTTPRequest(
              '{}:4151/pub?topic=chamelo'.format(settings.nsqdlookup_address),
              body=self.request.body.decode(),
              method="POST")
  
          client = tornado.httpclient.AsyncHTTPClient()
          client.fetch(request)
  
  
      def _valiate_required_fields(self):
          missing_fields = []
          all_required_fields = ['state', 'upsert', 'schema']
  
          for field in all_required_fields:
              if field not in self.json_args:
                  missing_fields.append(field)
  
          if len(missing_fields) &gt; 0:
              raise FieldException('Require following fields: %s' % ', '.join(missing_fields))
  
  
      @tornado.web.removeslash
      def post(self):
          # Validate if required keys are present
          try:
              self._valiate_required_fields()
          except FieldException as e:
              self.set_status(400)
              self.finish({
              "code": 400,
                  "message": str(e)
              })
  
          # Post request to nsqdlookup
          try:
              self._upload_to_nsq()
          except Error as e:
              self.set_status(400)
              self.finish({
              "code": 400,
                  "message": str(e)
              })
  
          # Everything is good
  
          self.set_status(200)
          self.finish({
              "code": 200,
              "message": "Ok"
          })
  </code></pre>