【问题标题】:Http Post data from Python to net core appHttp Post 数据从 Python 到 net core 应用程序
【发布时间】:2023-03-15 22:50:01
【问题描述】:

我正在尝试将文件从 Python 脚本发送到我的 .net 核心网络服务器。

在 Python 中,我使用 requests 库执行此操作,我的代码如下所示。

    filePath = "run-1.csv"
    with open(filePath, "rb") as postFile:
                file_dict = {filePath: postFile}
                response = requests.post(server_path + "/batchUpload/create", files=file_dict, verify=validate_sql)
    print (response.text)

这段代码执行得很好,我可以在我的网络服务器代码中看到请求很好,如下所示:

    [HttpPost]
    [Microsoft.AspNetCore.Authorization.AllowAnonymous]
    public string Create(IFormFile file) //Dictionary<string, IFormFile>
    {
                var ms = new MemoryStream();
                file.CopyTo(ms);
                var text = Encoding.ASCII.GetString(ms.ToArray());
                Debug.Print(text);
                return "s";
    }

但是,file 参数始终返回 null。 另外,从 postMan 获取数据时,我可以看到文件参数很好 我怀疑这个问题与 .net 核心模型绑定的工作方式有关,但不确定...... 关于如何让我的文件显示在服务器上的任何建议?

【问题讨论】:

  • 我认为您在这里的问题是,您首先从 python 寻址到应该是文件的文件,其次,您确定您只发送一个文件,并且这是正确的发送方式一个文件?

标签: python c# asp.net-mvc asp.net-core .net-core


【解决方案1】:

解决了我的问题 - 问题是在 Python 中,我使用实际文件名“./run1.csv”而不是文字字符串“file”将我的文件分配给我的上传字典 更新这个解决了我的问题。

file_dict = {"file": postFile}

这就是我相信上面提到的@nalnpir。 我通过从邮递员和我的 python 代码发布到http://httpbin.org/post 并比较响应来解决这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    相关资源
    最近更新 更多