Graphileon代理功能实现文件上传时报无JSON请求参数问题求助
问题根因
- 请求代理接口的
Content-Type配置错误:Graphileon的/proxy接口默认要求接收JSON格式的请求参数,你当前配置的mimetype: "multipart/form-data"会导致代理接口无法解析传入的参数,直接抛出找不到JSON参数的错误。 - FormData序列化错误:你直接把FormData对象拼接到JSON字符串中,会导致FormData被转为
[object FormData]这类无效字符串,无法正常传递文件内容到后端API。 - 代理参数格式不符合要求:Graphileon代理接口要求传入的JSON参数中,需要明确指定转发请求的headers,你没有指定自定义API需要的
multipart/form-data头,即使参数解析成功,转发后的请求也会不符合自定义API的要求。
修复方案
调整AJAX请求逻辑,先构造符合代理要求的JSON参数,再正确发送给/proxy接口,示例代码如下:
function createSchema(schemaType, formData) { // 同命名空间下K8s服务域名可正常解析 const targetUrl = `http://customapi:8080/create-schema/${schemaType}`; // 构造代理接口所需的JSON参数 const proxyPayload = JSON.stringify({ url: targetUrl, method: "POST", // 明确指定转发到自定义API的请求头 headers: { "Content-Type": "multipart/form-data" }, // 将FormData转为代理支持的键值对格式 formData: Array.from(formData.entries()).reduce((acc, [key, value]) => { acc[key] = value; return acc; }, {}) }); $.ajax({ url: "/proxy", type: "POST", data: proxyPayload, // 给代理接口指定正确的JSON内容类型 contentType: "application/json", cache: false, processData: false, success: function(data, status, xhr) { $("#result").html(""); $("#reset").show(); $("#schemaFileUpload").hide(); $("#result").append(`<h4>Create ${schemaType} Schema Success <span style='color:green' >${xhr.responseText} with Status code ${xhr.status}<span></h4> </br>`); $("#verticalSchemaFile").attr("disabled","disabled"); $("#customerSchemaFile").attr("disabled","disabled"); }, error: function(xhr, status, error) { $("#result").html(""); $("#reset").show(); $("#schemaFileUpload").hide(); $("#result").append(`${schemaType} create Schema Failed. <span style='color:red' >${xhr.responseText} with Status code ${xhr.status}<span> </br>`); $("#verticalSchemaFile").attr("disabled","disabled"); $("#customerSchemaFile").attr("disabled","disabled"); } }); }
额外排查点
如果修改后仍然报错,可以按以下顺序校验:
- 确认Graphileon的代理配置已经放开了
customapi:8080域名的转发权限,部分版本的Graphileon会默认禁止转发未配置的域名 - 确认自定义API的Pod端口8080已经在K8s Service中暴露,同命名空间下的Pod可以通过服务名正常访问
- 如果上传文件过大,确认Graphileon代理的请求体大小限制已经调整到符合上传文件的大小要求
内容的提问来源于stack exchange,提问作者Paras Bansal
相关产品推荐
相关产品推荐

