Skip to main content
Code Review

Return to Question

deleted 9 characters in body; edited tags; edited title; added 2 characters in body; edited body
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

Restful RESTful API echo server using pythonPython bottles

I just wrote an echo server that responds to a curl requests with the method used and the data sent. stickingSticking to GET and POST for now, but iI would like to know if theresthere's something iI can do to improve my RESTFULRESTful API.

ImI'm supposed to:

 1. Only be able to call the /data endpoint
 2. Only allow JSON parameters 
 3. Use best practices of coding a restful API
  1. Only be able to call the /data endpoint
  2. Only allow JSON parameters
  3. Use best practices of coding a RESTful API

Expected JSON response:

{
 "method""method": <HTTP METHOD REQUESTED>,
 "data""data": <HTTP PARAMETERS SENT IN THE REQUEST>
}

Here is my current code:

from bottle import run, post, request, response, get, route, put, delete
from json import dumps
import json
@get('/data')
def data():
 #if headers are not specified we can check if body is a json the following way
 #postdata = request.body.read()
 postdata = request.json
 try:
 #rv = {"method": request.method, "data": json.loads(postdata.decode('utf-8'))}
 rv = {"method": request.method, "data": postdata}
 except:
 raise ValueError
 response.content_type = 'application/json'
 return dumps(rv)
@post('/data')
def data():
 #postdata = request.body.read()
 postdata = request.json
 try:
 #rv = {"method": request.method, "data": json.loads(postdata.decode('utf-8'))}
 rv = {"method": request.method, "data": postdata}
 except:
 raise ValueError
 response.content_type = 'application/json'
 return dumps(rv)

Everything seems to work fine for now, i'm. I'm looking to improve my code so any feedback is appreciated.

Restful API echo server using python bottles

I just wrote an echo server that responds to a curl requests with the method used and the data sent. sticking to GET and POST for now but i would like to know if theres something i can do to improve my RESTFUL API

Im supposed to:

 1. Only be able to call the /data endpoint
 2. Only allow JSON parameters 
 3. Use best practices of coding a restful API

Expected JSON response:

{
 "method": <HTTP METHOD REQUESTED>,
 "data": <HTTP PARAMETERS SENT IN THE REQUEST>
}

Here is my current code:

from bottle import run, post, request, response, get, route, put, delete
from json import dumps
import json
@get('/data')
def data():
 #if headers are not specified we can check if body is a json the following way
 #postdata = request.body.read()
 postdata = request.json
 try:
 #rv = {"method": request.method, "data": json.loads(postdata.decode('utf-8'))}
 rv = {"method": request.method, "data": postdata}
 except:
 raise ValueError
 response.content_type = 'application/json'
 return dumps(rv)
@post('/data')
def data():
 #postdata = request.body.read()
 postdata = request.json
 try:
 #rv = {"method": request.method, "data": json.loads(postdata.decode('utf-8'))}
 rv = {"method": request.method, "data": postdata}
 except:
 raise ValueError
 response.content_type = 'application/json'
 return dumps(rv)

Everything seems to work fine for now, i'm looking to improve my code so any feedback is appreciated

RESTful API echo server using Python bottles

I just wrote an echo server that responds to curl requests with the method used and the data sent. Sticking to GET and POST for now, but I would like to know if there's something I can do to improve my RESTful API.

I'm supposed to:

  1. Only be able to call the /data endpoint
  2. Only allow JSON parameters
  3. Use best practices of coding a RESTful API

Expected JSON response:

{
 "method": <HTTP METHOD REQUESTED>,
 "data": <HTTP PARAMETERS SENT IN THE REQUEST>
}

Here is my current code:

from bottle import run, post, request, response, get, route, put, delete
from json import dumps
import json
@get('/data')
def data():
 #if headers are not specified we can check if body is a json the following way
 #postdata = request.body.read()
 postdata = request.json
 try:
 #rv = {"method": request.method, "data": json.loads(postdata.decode('utf-8'))}
 rv = {"method": request.method, "data": postdata}
 except:
 raise ValueError
 response.content_type = 'application/json'
 return dumps(rv)
@post('/data')
def data():
 #postdata = request.body.read()
 postdata = request.json
 try:
 #rv = {"method": request.method, "data": json.loads(postdata.decode('utf-8'))}
 rv = {"method": request.method, "data": postdata}
 except:
 raise ValueError
 response.content_type = 'application/json'
 return dumps(rv)

Everything seems to work fine for now. I'm looking to improve my code so any feedback is appreciated.

Source Link
Luis
  • 123
  • 2

Restful API echo server using python bottles

I just wrote an echo server that responds to a curl requests with the method used and the data sent. sticking to GET and POST for now but i would like to know if theres something i can do to improve my RESTFUL API

Im supposed to:

 1. Only be able to call the /data endpoint
 2. Only allow JSON parameters 
 3. Use best practices of coding a restful API

Expected JSON response:

{
 "method": <HTTP METHOD REQUESTED>,
 "data": <HTTP PARAMETERS SENT IN THE REQUEST>
}

Here is my current code:

from bottle import run, post, request, response, get, route, put, delete
from json import dumps
import json
@get('/data')
def data():
 #if headers are not specified we can check if body is a json the following way
 #postdata = request.body.read()
 postdata = request.json
 try:
 #rv = {"method": request.method, "data": json.loads(postdata.decode('utf-8'))}
 rv = {"method": request.method, "data": postdata}
 except:
 raise ValueError
 response.content_type = 'application/json'
 return dumps(rv)
@post('/data')
def data():
 #postdata = request.body.read()
 postdata = request.json
 try:
 #rv = {"method": request.method, "data": json.loads(postdata.decode('utf-8'))}
 rv = {"method": request.method, "data": postdata}
 except:
 raise ValueError
 response.content_type = 'application/json'
 return dumps(rv)

Everything seems to work fine for now, i'm looking to improve my code so any feedback is appreciated

lang-py

AltStyle によって変換されたページ (->オリジナル) /