FormData 如何在 multipart/form-data - Angular 中获取或设置边界
FormData how to get or set boundary in multipart/form-data - Angular
我有一个迷你应用程序,我必须在其中 post 从浏览器向端点发送表单数据。
这是我的 post:
var formData = new FormData();
formData.append('blobImage', blob, 'imagem' + (new Date()).getTime());
return $http({
method: 'POST',
url: api + '/url',
data: formData,
headers: {'Content-Type': 'multipart/form-data'}
})
边界似乎是由 formData 添加到参数中的,但是,我无法将其发送到 header,我该怎么办?
嗯,似乎 headers ContentType 应该是未定义的,以便添加正确的边界
正确的方法是不设置 Content-Type
header.
示例:
import { http } from '@angular/common/http'
function sendPostData(form: FormData) {
const url = `https://post-url-example.com/submit`;
const options = {
headers: new HttpHeaders({
Authorization: `Bearer auth-token`
})
};
return http.post(url, form, options);
}
进一步添加Pablo's answer.
当 http 请求 body 具有 FormData
类型时,angular 将延迟 Content-Type
header 分配给浏览器。 detectContentTypeHeader()
将 return null
FormData
请求 body 并且 angular 不会设置请求 header.
这是在 @angular/commons/http/src/xhr.ts
模块上。
// Auto-detect the Content-Type header if one isn't present already.
if (!req.headers.has('Content-Type')) {
const detectedType = req.detectContentTypeHeader();
// Sometimes Content-Type detection fails.
if (detectedType !== null) {
xhr.setRequestHeader('Content-Type', detectedType);
}
}
Content-Type 根据请求检测 body:
detectContentTypeHeader(): string|null {
// An empty body has no content type.
if (this.body === null) {
return null;
}
// FormData bodies rely on the browser's content type assignment.
if (isFormData(this.body)) {
return null;
}
// Blobs usually have their own content type. If it doesn't, then
// no type can be inferred.
if (isBlob(this.body)) {
return this.body.type || null;
}
// Array buffers have unknown contents and thus no type can be inferred.
if (isArrayBuffer(this.body)) {
return null;
}
// Technically, strings could be a form of JSON data, but it's safe enough
// to assume they're plain strings.
if (typeof this.body === 'string') {
return 'text/plain';
}
// `HttpUrlEncodedParams` has its own content-type.
if (this.body instanceof HttpParams) {
return 'application/x-www-form-urlencoded;charset=UTF-8';
}
// Arrays, objects, and numbers will be encoded as JSON.
if (typeof this.body === 'object' || typeof this.body === 'number' ||
Array.isArray(this.body)) {
return 'application/json';
}
// No type could be inferred.
return null;
}
来源:
我有一个迷你应用程序,我必须在其中 post 从浏览器向端点发送表单数据。
这是我的 post:
var formData = new FormData();
formData.append('blobImage', blob, 'imagem' + (new Date()).getTime());
return $http({
method: 'POST',
url: api + '/url',
data: formData,
headers: {'Content-Type': 'multipart/form-data'}
})
边界似乎是由 formData 添加到参数中的,但是,我无法将其发送到 header,我该怎么办?
嗯,似乎 headers ContentType 应该是未定义的,以便添加正确的边界
正确的方法是不设置 Content-Type
header.
示例:
import { http } from '@angular/common/http'
function sendPostData(form: FormData) {
const url = `https://post-url-example.com/submit`;
const options = {
headers: new HttpHeaders({
Authorization: `Bearer auth-token`
})
};
return http.post(url, form, options);
}
进一步添加Pablo's answer.
当 http 请求 body 具有 FormData
类型时,angular 将延迟 Content-Type
header 分配给浏览器。 detectContentTypeHeader()
将 return null
FormData
请求 body 并且 angular 不会设置请求 header.
这是在 @angular/commons/http/src/xhr.ts
模块上。
// Auto-detect the Content-Type header if one isn't present already.
if (!req.headers.has('Content-Type')) {
const detectedType = req.detectContentTypeHeader();
// Sometimes Content-Type detection fails.
if (detectedType !== null) {
xhr.setRequestHeader('Content-Type', detectedType);
}
}
Content-Type 根据请求检测 body:
detectContentTypeHeader(): string|null {
// An empty body has no content type.
if (this.body === null) {
return null;
}
// FormData bodies rely on the browser's content type assignment.
if (isFormData(this.body)) {
return null;
}
// Blobs usually have their own content type. If it doesn't, then
// no type can be inferred.
if (isBlob(this.body)) {
return this.body.type || null;
}
// Array buffers have unknown contents and thus no type can be inferred.
if (isArrayBuffer(this.body)) {
return null;
}
// Technically, strings could be a form of JSON data, but it's safe enough
// to assume they're plain strings.
if (typeof this.body === 'string') {
return 'text/plain';
}
// `HttpUrlEncodedParams` has its own content-type.
if (this.body instanceof HttpParams) {
return 'application/x-www-form-urlencoded;charset=UTF-8';
}
// Arrays, objects, and numbers will be encoded as JSON.
if (typeof this.body === 'object' || typeof this.body === 'number' ||
Array.isArray(this.body)) {
return 'application/json';
}
// No type could be inferred.
return null;
}
来源: