使用Python的请求库执行URL重写的问题

时间:2018-08-18 16:04:35

标签: python flask python-requests

我正在尝试使用“请求”将发送到Flask端点的请求代理到另一个URL。

我想传递所有已发布的数据,因此我在json=get_json()中使用requests.post。但是,当初始请求为GET时,它没有任何JSON,因此出现“错误:无法解码JSON对象:期望值:第1行第1列(字符0)”错误。

我改用json=request.data,但收到“ TypeError:'bytes'类型的对象不可JSON序列化”。

如何在不假设为JSON的情况下获取数据?如何在不假定为JSON的情况下将数据传递给请求?

from flask import request
import requests

@app.route("/pas/<path:arg>", methods=("GET", "POST", "PUT", "DELETE"))
def proxy(arg):
    url = f"http://{config.pasUrl}:{config.pasPort}/{arg}?{request.query_string.decode('utf-8')}"
    out_request = requests.request(
        method=request.method,
        url=url,
        headers=request.headers,
        json=request.get_json(),
    )
    return out_request.text

1 个答案:

答案 0 :(得分:2)

$ ./bin/arr3d_dyn_ptr2d m[ 0]: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 m[ 1]: 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ... m[23]: 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 替换为valgrind

$ valgrind ./bin/arr3d_dyn_ptr2d ==25240== Memcheck, a memory error detector ==25240== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. ==25240== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info ==25240== Command: ./bin/arr3d_dyn_ptr2d ==25240== m[ 0]: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 m[ 1]: 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ... m[23]: 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 ==25240== ==25240== HEAP SUMMARY: ==25240== in use at exit: 0 bytes in 0 blocks ==25240== total heap usage: 3 allocs, 3 frees, 22,400 bytes allocated ==25240== ==25240== All heap blocks were freed -- no leaks are possible ==25240== ==25240== For counts of detected and suppressed errors, rerun with: -v ==25240== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) takes a dictionary, list of tuples, bytes, or file-like objectjson=request.get_json()参数和Flask的data=request.get_data()返回请求正文的原始字节。