使用AngularJS客户端和Flask服务器的REST调用中的斜杠

0 投票
1 回答
555 浏览
提问于 2025-04-18 04:35

我正在从一个AngularJs客户端调用一个REST Flask服务器。

在一次调用中,我的一个ID(键)可能包含一个斜杠。

如果没有斜杠,这个调用就能正常工作。

但如果有斜杠,我就会收到404未找到的错误。

我尝试过对这个键进行编码(用encodeURIComponent),也尝试过转义(用escape),但还是出现404错误。

这是我在Flask中的代码:

@app.route('/user/<string:uid>/subuser/<string:subuser>/key/<string:key>', methods=['DELETE'])
def deleteSubuserKey(uid, subuser, key):
    Log.err("deleteSubuserKey")
    return ...

这是我在AngularJS中的代码:

$scope.uri = ctrlURL+"S3/user/"+user_id+"/subuser/"+subuser+"/key/"+key;
$http({method: "delete", url: $scope.uri }).
     success(function (data, status) {
                ...
            }).
     error(function (data, status) {
                ...
            });

有没有人知道该怎么解决?

1 个回答

0

感谢Brian的评论,我解决了我的问题。

当我对斜杠进行编码时,我的请求直接被Apache拒绝了。

我在我的Apache配置文件中添加了AllowEncodedSlashes指令:

AllowEncodedSlashes NoDecode

使用以下编码的密钥:

encodeURIComponent(key)

这样就可以正常工作了。

撰写回答