chenshuai2144 / openapi2typescript

一个被大范围使用的小工具
297 stars 133 forks source link

生成的application/x-www-form-urlencoded类型request在浏览器中发送的依然是json #146

Open tc-imba opened 3 months ago

tc-imba commented 3 months ago

Related to #140

相同的openapi.json文件,新版本输出的request浏览器发送的contentType仍然错误

v1.9.1 浏览器发送 Content-Type: application/x-www-form-urlencoded 生成的是

/** Auth:Jwt.Login POST /api/v1/auth/jwt/login */
export async function authJwtLoginApiV1AuthJwtLoginPost(
  body: API.BodyAuthJwtLoginApiV1AuthJwtLoginPost,
  options?: { [key: string]: any },
) {
  const formData = new FormData();

  Object.keys(body).forEach((ele) => {
    const item = (body as any)[ele];

    if (item !== undefined && item !== null) {
      if (typeof item === 'object' && !(item instanceof File)) {
        if (item instanceof Array) {
          item.forEach((f) => formData.append(ele, f || ''));
        } else {
          formData.append(ele, JSON.stringify(item));
        }
      } else {
        formData.append(ele, item);
      }
    }
  });

  return request<API.BearerResponse>('/api/v1/auth/jwt/login', {
    method: 'POST',
    data: formData,
    ...(options || {}),
  });
}

v1.11.1 浏览器发送 Content-Type: application/json 生成的是

/** Auth:Jwt.Login POST /api/v1/auth/jwt/login */
export async function authJwtLoginApiV1AuthJwtLoginPost(
  body: API.BodyAuthJwtLoginApiV1AuthJwtLoginPost,
  options?: { [key: string]: any },
) {
  return request<API.BearerResponse>('/api/v1/auth/jwt/login', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: body,
    ...(options || {}),
  });
}