[英]JSON in a POST request is unsupported for Restlet
我们正在创建一个小型项目,在该项目中,烧瓶是一个前端,而restlet是基于WebService的。
我们尝试将登录数据作为JSON从Flask发送到Restlet:
def login():
error = None
if request.method == 'POST':
payload = {'username' : request.form['username'], 'password' : request.form['password']}
headers = {'Content-Type': 'application/json'}
req = requests.post(WEBSERVICE_IP + '/login', data=json.dumps(payload), headers=headers)
(...)
基于Flask的网站大喊:
ValueError: No JSON object could be decoded
我们不知道如何协调flask和restlet之间的通信。
编辑(格林尼治标准时间22-04 10:08 pm) :我发现响应是:
<html>
(...)
Unsupported Media Type
The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method
(...)
</html>
编辑(格林尼治标准时间22-04 11:26 pm) :我仍然不确定为什么,但是我认为它可能是JSON格式的。 更正我的代码后,它将发送正确的JSON(表示为JSONLint),我仍然收到相同的消息。 有人知道如何在Python中创建JSONObject吗? WebService有方法:
@Post("json")
public JSONObject verifyAccount(JSONObject dane){
编辑(格林尼治标准时间23-04 7:26 pm) :好的。 因此,我们几乎可以确定这是不可见标头的问题。 任何人都可以确认此处python代码中的标头创建正确吗?
编辑(格林尼治标准时间24-04 5:40 pm) :问题仍然存在。 正如其他建议一样,我将请求改回了urllib2。 这有助于解决第一件事-“价值问题”。 现在浏览器有
urllib2.HTTPError
HTTPError: HTTP Error 415: Unsupported Media Type
POST现在请求:
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
payload = {"Login": request.form['username'],
"Haslo": request.form['haslo']}
data = json.dumps(payload)
clen = len(data)
req = urllib2.Request(WEBSERVICE_IP + '/login', data,
{'Content-Type': 'application/json', 'Content-Length': clen})
f = urllib2.urlopen(req)
response = f.read()
f.close()
编辑(格林尼治标准时间24-04 6:20 pm) :
Wireshark捕获了POST请求,看起来还不错。
如果data
是字典,则请求将对其进行序列化。 您想改为传递一个字符串:
import json
req = requests.post(WEBSERVICE_IP + '/login', data=json.dumps(payload), ...
好。 解决方案比我想象的要容易。
问题出在WebService端。 通过将JSONObject更改为JsonRepresentation可以解决此问题:
@Post("json")
public JSONObject verifyAccount(JsonRepresentation data){
除了Blender的观点(这很可能是罪魁祸首)之外,值得一提的是,应将content-type设置为application/json
而不是json
。
要添加危害响应,我必须将@Post("json")
为@Post("application/json")
:
@Post("application/json")
public JSONObject verifyAccount(JsonRepresentation data){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.