gpt4 book ai didi

python - 使用 Python 向 Glassfish 服务器发出 RESTFUL POST 请求

转载 作者:行者123 更新时间:2023-12-01 05:58:52 25 4
gpt4 key购买 nike

我在尝试对 Glassfish 上运行的 Web 服务进行 Python REST POST 时遇到困难。我已经验证 POST 使用 CURL 可以正常工作,但使用 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

控制台输出,

Unexpected character ('l' (code 108)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: org.apache.catalina.connector.CoyoteInputStream@18f494d; line: 1, column: 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'})

关于python - 使用 Python 向 Glassfish 服务器发出 RESTFUL POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11379050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com