def do_POST(self): # Set response code to 'Created' self._set_headers(201) content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) # Convert JSON string to a Python dictionary post_body = json.loads(post_body) # Parse the URL (resource, id) = self.parse_url(self.path) # Initialize new item new_item = None # Add a new animal to the list. Don't worry about # the orange squiggle, you'll define the create_animal # function next. if resource == "animals": new_item = create_animal(post_body) if resource == "locations": new_item = create_location(post_body) if resource == "employees": new_item = create_employee(post_body) if resource == "customers": new_item = create_customer(post_body) # Encode the new animal and send in response self.wfile.write(f"{new_item}".encode())
def do_POST(self): self._set_headers(201) content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) # Convert JSON string to a Python dictionary post_body = json.loads(post_body) # Parse the URL (resource, id) = self.parse_url(self.path) new_animal = None new_location = None new_employee = None new_customer = None if resource == "animals": new_animal = create_animal(post_body) self.wfile.write(f"{new_animal}".encode()) if resource == "locations": new_location = create_location(post_body) self.wfile.write(f"{new_location}".encode()) if resource == "employees": new_employee = create_employee(post_body) self.wfile.write(f"{new_employee}".encode()) if resource == "customers": new_customer = create_customer(post_body) self.wfile.write(f"{new_customer}".encode())
def do_POST(self): self._set_headers(201) # You should be able to explain the purpose of a 201 status code. content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) # Convert JSON string to a Python dictionary post_body = json.loads(post_body) # Parse the URL (resource, id) = self.parse_url(self.path) # Initialize new animal new_animal = None new_customer = None new_employee = None new_location = None # Add a new animal to the list. Don't worry about # the orange squiggle, you'll define the create_animal # function next. if resource == "animals": new_animal = create_animal(post_body) # Encode the new animal and send in response self.wfile.write(f"{new_animal}".encode()) if resource == "customers": new_customer = create_customer(post_body) # Encode the new customer and send in response self.wfile.write(f"{new_customer}".encode()) if resource == "employees": new_employee = create_employee(post_body) # Encode the new employee and send in response self.wfile.write(f"{new_employee}".encode()) if resource == "locations": new_location = create_location(post_body) # Encode the new location and send in response self.wfile.write(f"{new_location}".encode())
def do_POST(self): self._set_headers(201) content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) # Convert JSON string to a Python dictionary post_body = json.loads(post_body) # Parse the URL. url returns a tuple with 2 things in it. We are unpacking it on the right side of equal sign. (resource, id) = self.parse_url(self.path) # Initialize new animal new_object = None # We are checking the resource on line 121. Add a new animal to the list. Don't worry about # the orange squiggle, you'll define the create_animal # function next. #"post_body" is the body of the post/object in the Animals array of objects if resource == "animals": new_object = create_animal(post_body) # Encode the new animal and send in response #locations post request # new_object = None if resource == "locations": new_object = create_location(post_body) #employees post request # new_object = None if resource == "employees": new_object = create_employee(post_body) #customers post request # new_object = None if resource == "customers": new_object = create_customer(post_body) self.wfile.write(f"{new_object}".encode())
def do_POST(self): # Set response code to 'Created' self._set_headers(201) content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) post_body = json.loads(post_body) resource, id = self.parse_url(self.path) new_item = None if resource == "animals": new_item = create_animal(post_body) elif resource == "locations": new_item = create_location(post_body) elif resource == "employees": new_item = create_employee(post_body) elif resource == "customers": new_item = create_customer(post_body) self.wfile.write(f"{new_item}".encode())