gpt4 book ai didi

powershell - 将JSON文件上传到Web API

转载 作者:行者123 更新时间:2023-12-03 00:06:34 31 4
gpt4 key购买 nike

我正在尝试使用Powershell Core将JSON文件上传到.Net Core 3 WebApp,但是无法正常工作。 "file"始终为null。

在WebApp的代码下方:

[HttpPost]
public async Task<ActionResult> UploadFile(IFormFile file)
{
if (!ModelState.IsValid)
return BadRequest();

var path = Path.GetTempFileName();
var stream = new FileStream(path, FileMode.Create);
await file.CopyToAsync(stream);
string FileName = file.FileName;

// .. parsing the file

return Ok();
}

以及来自Powershell的实际要求:
$file = '/fileToUpload.json'
Invoke-RestMethod -Uri 'https://localhost:5001/uploadfile' -Method Post -InFile $file -ContentType 'application/json'

但是此请求始终返回以下错误:
Invoke-RestMethod : System.NullReferenceException: Object reference not set to an instance of an object.

当在方法中设置断点时,我可以看到"file"始终为空。

最初,我从一个接受正文中JSON数据的方法开始,但是在使用Invoke-RestMethod时,Post请求的正文中似乎存在某种限制,因此我不得不想出另一种解决方案来解决此“限制'。
我在使用Postman时没有遇到限制问题,所以我想它与Powershell有关。如果有人知道如何解决此“正文限制”问题,那将更大,因为我更喜欢在正文中发布JSON数据而不是发布文件。

希望有人能帮助我。
提前致谢。

最佳答案

您不应该使用-Co​​ntentType'application / json上传文件。
See Example 4: Simplified Multipart/Form-Data Submission

关于powershell - 将JSON文件上传到Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60229059/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com