def on_post(self, req, resp, nickname): if req.content_length in (None, 0): return body = req.stream.read() if not body: raise falcon.HTTP_BAD_REQUEST() try: doc = json.loads(body.decode('utf-8')) except (ValueError, UnicodeDecodeError): raise falcon.HTTP_BAD_REQUEST() user_dao = userDAO.UserDAO() resp_body, resp_status = user_dao.create_user(nickname, doc) resp.body = json.dumps(resp_body) resp.status = resp_status
def on_post(self, req, resp, slug): if req.content_length in (None, 0): return body = req.stream.read() if not body: raise falcon.HTTP_BAD_REQUEST() try: doc = json.loads(body.decode('utf-8')) except (ValueError, UnicodeDecodeError): raise falcon.HTTP_BAD_REQUEST() forum_dao = forumDAO.ForumDAO() resp_body, resp_status = forum_dao.create_thread(slug, doc) resp.body = json.dumps(resp_body) resp.status = resp_status
def on_post(self, req, resp, pid): if req.content_length in (None, 0): return body = req.stream.read() if not body: raise falcon.HTTP_BAD_REQUEST() try: doc = json.loads(body.decode('utf-8')) except (ValueError, UnicodeDecodeError): raise falcon.HTTP_BAD_REQUEST() post_dao = postDAO.PostDAO() resp_body, resp_status = post_dao.edit_post(pid, doc) resp.body = json.dumps(resp_body) resp.status = resp_status
def on_post(self, req, res): user = User.find_by_username(req.context["session"], req.media["username"]) if self.auth_manager.verify_password(req.media["password"], user.password.encode("utf-8")): user.sid = self.auth_manager.generate_session_id() user.token = self.auth_manager.encrypt(user.sid).decode("utf-8") res.body = json.dumps({"token": user.token}) else: raise falcon.HTTP_BAD_REQUEST()
def json_body(req): if not req.content_length: return {} try: raw_json = req.stream.read() except Exception: raise falcon.HTTP_BAD_REQUEST('Empty request body. A valid JSON ' 'document is required.') try: json_data = json.loads(raw_json, 'utf-8') except ValueError: raise falcon.HTTPError(falcon.HTTP_753, 'Malformed JSON') return json_data
def get_raw_sentences_from_payload(req): """An helper function to extract a list of sentence from request payload. If ``Content-Type`` is ``application/json`` -> parse JSON payload and grab `sentences` property. If ``Content-Type`` is ``text/plain`` -> split payload by newline char. Args: req (falcon.request.Request): request. Raises: ``falcon.HTTPBadRequest`` - Mal-formatted request payload. ``falcon.HTTPError`` - internal server errors (e.g JSON parsing). Returns: list """ if req.content_length in (None, 0): raise falcon.HTTPBadRequest('Request payload is empty.') content_type = req.content_type.lower() # curl -X POST -H "Content-Type: text/plain" --data-binary "@sentences.txt" <URL> if content_type == 'text/plain': sentences = req.bounded_stream.read().decode('utf-8').split('\n') # curl -X POST -H "Content-Type: application/json" --data-binary "@sentences.json" <URL> elif content_type == 'application/json': try: sentences = json.loads(req.bounded_stream.read().decode('utf-8')) except (ValueError, UnicodeDecodeError): raise falcon.HTTPError( falcon.HTTP_753, 'Malformed JSON', 'Could not decode the request body. The ' 'JSON was incorrect or not encoded as ' 'UTF-8.') if isinstance(sentences, dict): sentences = sentences['sentences'] else: raise falcon.HTTP_BAD_REQUEST( f'Invalid content-type: {req.content_type}') sentences = [sent for sent in sentences if sent != ''] if not sentences: raise falcon.HTTPBadRequest('Request payload is empty.') return sentences