不管是overrideFetch还是createFetch,headers选项设置不起作用
happyybm opened this issue · comments
不管是overrideFetch还是createFetch,把headers设置成 application/x-www-form-urlencoded 都不起作用,只有写在fetch的第二个参数中有用,且content-type属性必需是小驼峰contentType才行
以下代码设置后是不起作用的:
export const fetch = createFetch(
{
/** 'prefix' 前缀,统一设置 url 前缀,默认是 '' */
// prefix: 'https://rap2api.taobao.org/app/mock/3402',
fetchOption: {
/** 全局配置请求 content-type,默认是 'application/json' */
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
credentials: 'same-origin',
},
},
{ fetchType: FetchType.AUTO },
);
以下代码设置是可以的:
fetch['POST/api/detail'](
{ uid: 1 },
{
contentType: 'application/x-www-form-urlencoded',
}
)
https://github.com/thx/rapper/blob/master/src/runtime/commonLib.ts#L155-L172
extra = extra || {};
let urlWithParams = url;
const init: RequestInit = { ...(fetchOption || {}), method };
if (method === 'GET') {
const qs = stringifyQueryString(params, extra.queryStringFn);
urlWithParams = qs ? url + '?' + qs : url;
} else if (
['POST', 'DELETE', 'PUT'].includes(method) &&
extra.contentType === 'application/x-www-form-urlencoded'
) {
init.body = stringifyQueryString(params, extra.queryStringFn);
} else if (
['POST', 'DELETE', 'PUT'].includes(method) &&
extra.contentType === 'multipart/form-data'
) {
const formdata = new FormData();
params &&
Object.keys(params).forEach(key => {
formdata.append(key, params[key]);
});
init.body = formdata;
} else {
init.body = typeof params === 'object' ? JSON.stringify(params) : params;
}
你以为他会给你从object转URLSearchParams,其实并没有 😢
慢慢等作者更新吧
或者(不还是没有
const querystring = require('querystring');
fetch['POST/api/detail'](querystring.stringify({ uid: 1 }) as unknown as any);
最新版 v1.1.1 已修复此问题,感谢提出bug