2012-07-07 119 views
1

我在尝试对运行在Glassfish上的web服务进行Python REST POST时遇到困难。我已经证实,使用CURL,POST可以正常工作,但对Python没有好运。向Glassfish服务器发送Python请求的RESTFUL POST

这是可以正常工作的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 

这里是Python代码,以使POST请求从控制台

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') at [Source:org.ap [email protected];行:1,柱:2]

希望有人能阐明该问题的一些光。

感谢 尼克

回答

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'}) 
+0

菲尔喜服务器,这完美地工作。 – 2012-07-08 12:59:20

+0

@om_kcin非常欢迎SO。记得“接受”有效的答案,以获得更好的答复率 – 2012-07-08 15:08:49

+0

嗨菲尔,接受的答案(我认为)再次感谢您的帮助 – 2012-07-14 14:38:33