文件上传的 XUnit 测试导致异常,但端点在我的站点中使用时有效
XUnit test for file upload causes an exception though the endpoint works when used in my site
使用失眠用RestSharp创建代码:
var client = new RestClient("https://localhost:44372/api/Box/Upload");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001");
request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"LiterallyAnything\"\r\n\r\n", ParameterType.RequestBody);
request.AddFile("MyFileName", "MyFileAndPath");
IRestResponse response = client.Execute(request);
这很好地命中了 .net Core 2.1 端点:
[HttpPost("Upload")]
[ProducesResponseType(typeof(ActionResult), 201)]
[ProducesResponseType(typeof(ActionResult), 400)]
[ProducesResponseType(typeof(ActionResult), 500)]
public async Task<IActionResult> Upload() {
ActionResult retVal = StatusCode(500);
try {
if (Request.Form.Files.Count() != 1) { //<----Throws exception here
我收到以下错误:
Unexpected end of Stream, the content may have already been read by another component.
我检查过是否有某种文件锁定(没有)。该文件绝对存在。当在单元测试中 运行 时,它似乎处理得不太好。
什么给了?
当您使用 IRestRequest.AddFile
的任何重载时,它会向请求添加一种特殊类型的参数,称为 FileParameter
。当RestClient
发现请求中的文件列表不为空时,会自动为你形成一个mult-part表单请求,并设置所有需要的headers.
一般情况下,使用RestSharp需要手动添加headers和技术参数的情况非常少。因此,我们在文档中的任何地方都没有向请求 headers 添加诸如内容类型之类的内容,因为它总是根据请求参数、body、文件等自动为您完成。
关于 Insomnia 客户端,他们使用 Kong 的 httpsnippet
工具生成代码片段。 This is their code for RestSharp, which is extremely simplistic and definitely doesn't fit our recommended usage, so I would recommend checking our docs.
使用失眠用RestSharp创建代码:
var client = new RestClient("https://localhost:44372/api/Box/Upload");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001");
request.AddParameter("multipart/form-data; boundary=---011000010111000001101001", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"LiterallyAnything\"\r\n\r\n", ParameterType.RequestBody);
request.AddFile("MyFileName", "MyFileAndPath");
IRestResponse response = client.Execute(request);
这很好地命中了 .net Core 2.1 端点:
[HttpPost("Upload")]
[ProducesResponseType(typeof(ActionResult), 201)]
[ProducesResponseType(typeof(ActionResult), 400)]
[ProducesResponseType(typeof(ActionResult), 500)]
public async Task<IActionResult> Upload() {
ActionResult retVal = StatusCode(500);
try {
if (Request.Form.Files.Count() != 1) { //<----Throws exception here
我收到以下错误:
Unexpected end of Stream, the content may have already been read by another component.
我检查过是否有某种文件锁定(没有)。该文件绝对存在。当在单元测试中 运行 时,它似乎处理得不太好。
什么给了?
当您使用 IRestRequest.AddFile
的任何重载时,它会向请求添加一种特殊类型的参数,称为 FileParameter
。当RestClient
发现请求中的文件列表不为空时,会自动为你形成一个mult-part表单请求,并设置所有需要的headers.
一般情况下,使用RestSharp需要手动添加headers和技术参数的情况非常少。因此,我们在文档中的任何地方都没有向请求 headers 添加诸如内容类型之类的内容,因为它总是根据请求参数、body、文件等自动为您完成。
关于 Insomnia 客户端,他们使用 Kong 的 httpsnippet
工具生成代码片段。 This is their code for RestSharp, which is extremely simplistic and definitely doesn't fit our recommended usage, so I would recommend checking our docs.