Python:将请求放置到剩余api时发送错误请求400错误

时间:2019-12-07 17:43:40

标签: python rest python-requests python-responses

我正在向Rest Api发送一个ValueError: Output of generator should be a tuple `(x, y, sample_weight)` or `(x, y)`. Found: [[[[-5.30867195e+01 -6.81702271e+01 2.66113968e+01] [-5.04675522e+01 -6.62993927e+01 2.90434952e+01] [-4.78483849e+01 -6.44285583e+01 3.14755783e+01] ... 请求,以根据用户名更新用户信息,并且出现以下错误

Put

API简介如下

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>

我按如下方式创建我的请求,

/api/users/username GET, PUT    Token

如果需要更多信息,我可以提供,其余的api代码段也不错

headers = {'Content-Type': 'application/json',
'Token': token}

datap = [{'firstname': 'aaa1',
'lastname': 'aaa1',
'phone': '222222'}]

r = requests.put('http://127.0.0.1:8080/api/users/user1a', data=datap, headers=headers)
print(r.text)

现在将json封装到@bp.route('/users/<username>', methods=(['GET', 'PUT'])) @token_required def username(username): if request.method == 'GET': return get_specific_user(username) if request.method == 'PUT': return put_specific_user(username, request) def put_specific_user(username, request): allowed_fields = ('firstname', 'lastname', 'phone') if not request.is_json: return jsonify({'status': 'FAILURE', 'message': 'Bad Request'}), 400 中之后,我遇到了错误,因此它的请求也不错了

[]

1 个答案:

答案 0 :(得分:0)

已更改

requests.put('http://127.0.0.1:8080/api/users/user1a', data=datap, headers=headers)

requests.put('http://127.0.0.1:8080/api/users/user1a', data=json.dumps(datap), headers=headers)

PS:我不知道为什么有人会给我-1来解决这个问题,可以通过mod来研究一下。这样我就不用再做了。