-1

I have some simple code, like this:

import json
from bottle import route, request,run
@route('/process_json',methods='POST')
def data_process():
    data = json.loads(request.data)
    username = data['username']
    password = data['password']
run(host='localhost', port=8080, debug=True)

I would like to send a data in json format, like this:

$ curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d 
'{"username":"fooba","password":"1234"}' url

However, I have this problem:

File "/usr/lib/python2.7/dist-packages/bottle.py", line 862, 
  in _handle return route.call(**args) 
File "/usr/lib/python2.7/dist-packages/bottle.py", line 1729, 
  in wrapper rv = callback(*a, **ka) 
File "testtest.py", line 5, 
  in data_process data = json.loads(request.data) 
File "/usr/lib/python2.7/dist-packages/bottle.py", line 1391, 
  in getattr raise AttributeError('Attribute %r not defined.' % name) 
   AttributeError: Attribute 'data' not defined

I also tried add at the beginning like here(http://bottlepy.org/docs/dev/tutorial.html#html-form-handling): return ''' Username: Password: ''' But it also doesn't work.

szarad
  • 81
  • 1
  • 7

1 Answers1

0

It is good, you have provided the bottle application code. It was broken.

Following modification works:

import json
from bottle import route, request, run


@route('/process_json', method="POST")
def data_process():
    data = json.load(request.body)
    print "data", data
    username = data['username']
    password = data['password']
run(host='localhost', port=8080, debug=True)

Testing with curl

$ curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST  -d '{"username": "fooba", "password": "1234"}' http://localhost:8080/process_json
HTTP/1.0 200 OK
Date: Mon, 14 Jul 2014 16:18:25 GMT
Server: WSGIServer/0.1 Python/2.7.6
Content-Length: 0
Content-Type: text/html; charset=UTF-8

Testing with HTTPie

$ http POST http://localhost:8080/process_json username=jan password=pswd
HTTP/1.0 200 OK
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Mon, 14 Jul 2014 16:15:16 GMT
Server: WSGIServer/0.1 Python/2.7.6

Actually, I was playing with your example to find out, how it would look like in http command. It is really much simpler and using --verbose we may check, it really forms valid JSON payload:

$ http --verbose POST http://localhost:8080/process_json Accept:application/json 
Content_type:application/json username=jan password=pswd                                                  
POST /process_json HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Authorization: Basic amFuOnZsY2luc2t5
Content-Length: 39
Content-Type: application/json; charset=utf-8
Content_type: application/json
Host: localhost:8080
User-Agent: HTTPie/0.8.0

{
    "password": "pswd", 
    "username": "jan"
}

HTTP/1.0 200 OK
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Mon, 14 Jul 2014 16:21:02 GMT
Server: WSGIServer/0.1 Python/2.7.6

The shortest form is:

$ http :8080/process_json username=jan password=pswd
HTTP/1.0 200 OK
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Mon, 14 Jul 2014 16:25:12 GMT
Server: WSGIServer/0.1 Python/2.7.6

as http://localhost:8008 can be shortened to :8080 (the same applies to curl)

and if there is a payload, default method is POST.

Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98