RESTFUL POST与Glassfish服务器的Python请求

时间:2012-07-07 22:11:40

标签: python rest glassfish

我在尝试对在Glassfish上运行的Web服务进行Python REST POST时遇到了困难。我已经使用CURL验证了POST工作正常,但是对Python没有运气。

以下是正常的CURL请求。

curl -X POST -H "Content-Type: application/json" -d '{"id":1,"lastname":"smith"}' 
  http://192.168.0.20:8080/field1/resources/com.field1entity.field1

以下是发出POST请求的Python代码

import urllib
import httplib2

def call():
http = httplib2.Http()

url = 'http://192.168.0.20:8080/field1/resources/com.field1entity.field1'

params = urllib.urlencode({"id":11111,"lastname":"oojamalip"})

response, content = http.request(url, 'POST', params, headers={'Content-type':'application/json'})
print "lets stop here to have a looksy at the variables"                               
print content

if __name__ == '__main__':

namesPage = call()
print namesPage

从控制台输出

  

意外字符('l'(代码108)):预期有效值(数字,字符串,数组,对象,'true','false'或'null')    在[来源:org.apache.catalina.connector.CoyoteInputStream@18f494d; line:1,column:2]

希望有人可以解决这个问题。

感谢 尼克

1 个答案:

答案 0 :(得分:2)

你是url编码的婴儿车,然后告诉服务器它是json编码

import json

params = json.dumps({"id":11111,"lastname":"oojamalip"})
# then
response, content = http.request(url, 'POST', body=params, headers={'Content-type':'application/json'})