如何通过curl发送json的数据?

时间:2014-07-14 10:28:32

标签: python json curl data-processing

我有一些简单的代码,如下:

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)

我想以json格式发送数据,如下所示:

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

但是,我有这个问题:

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

我也尝试像这里一样添加(http://bottlepy.org/docs/dev/tutorial.html#html-form-handling):         返回''''                              用户名:                 密码:                                   ''' 但它也不起作用。

1 个答案:

答案 0 :(得分:0)

很好,你提供了瓶子应用程序代码。它坏了。

以下修改工作:

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)

使用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

使用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

实际上,我正在玩你的例子来找出它在http命令中的样子。它实际上要简单得多,使用--verbose我们可以检查,它确实形成了有效的JSON有效载荷:

$ 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

最短的形式是:

$ 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

http://localhost:8008可以缩短为:8080(同样适用于curl

如果有有效负载,则默认方法为POST