【发布时间】:2020-06-28 15:40:07
【问题描述】:
我已经浏览了无数帖子,我只是被这个应该如此简单的东西难住了。
let formData = new FormData();
formData.append('message', payload.message);
formData.append('attachment', this.state.attachment);
let options = {
method: 'post',
body: formData
}
fetch('api/contact', options);
然后我得到了非常熟悉的 415 错误:
Request URL: https://localhost:44348/api/contact
Request Method: POST
Status Code: 415
Remote Address: [::1]:44348
Referrer Policy: no-referrer-when-downgrade
我知道不要设置我的 Content-Type 甚至标题,包括边界在内的一切看起来都很好:
请求标头
:authority: localhost:44348
:method: POST
:path: /api/contact
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
cache-control: no-cache
content-length: 10269
content-type: multipart/form-data; boundary=----WebKitFormBoundary5iTqTV6AaKla3BMA
origin: https://localhost:44348
pragma: no-cache
referer: https://localhost:44348/contact
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36
表单数据
message: asdasd
attachment: (binary)
这与我之前用于旧 jQuery 网站的 API 相同,并且运行良好。有什么想法吗?
【问题讨论】:
-
选项中
formData之后的;是否是问题中的拼写错误? -
我看到你的内容类型是正常的,你能更清楚地告诉我变量“this.state.attachment”吗?在控制台 Chrome 中,您能否在 Form Data 中查看您的请求并单击查看源以获取有关数据“附件”的更多信息?
-
通常情况下,您对哪种内容类型(例如:JSON)的请求的响应是什么?
-
您应该提供更多详细信息(API 端点后面的 HTTP 实现是什么?jQuery 请求是什么样的?)
-
我更新了我的 .Net Core 版本……它区分大小写,而旧版本则不区分大小写。浪费了这么多时间:P
标签: fetch multipartform-data form-data http-status-code-415