GraphQL文件上传报错:HttpPostRequestEncoder异常求助
GraphQL图片上传报错:FileNotFoundException 排查与解决
我在使用Karate框架通过GraphQL上传图片bird.jpeg时,遇到如下错误:
io.netty.handler.codec.http.multipart.HttpPostRequestEncoder$ErrorDataEncoderException: java.io.FileNotFoundException
相关代码信息
传入数据
{ "file":"bird.jpeg" }
GraphQL查询语句
mutation UpdateProfilePicture ($file: Upload!) { singleUpload(file: $file){ done } }
Karate Feature文件代码
Feature: get parent players Background: * url baseUrl Scenario: get parent players details * def result = call read(loginPath) * def accessToken = result.accessToken * print "Access Token : ", accessToken And header Authorization = 'Bearer ' + accessToken Given def query = read(gqlBase +'updateProfilePicture.gql') And request { query: '#(query)'} * multipart file file = {read:'bird.jpeg', filename:'bird.jpeg',Content-type:'image/jpeg'} When method post Then status 200 * print 'response: ', response.data
排查与解决步骤
- 核对文件路径
- 确保
bird.jpeg在Karate可读取的路径下:如果用相对路径,基准是当前Feature文件所在目录;也可以直接使用绝对路径明确指定位置,比如read:'src/test/resources/bird.jpeg'。
- 确保
- 确认文件存在性
- 手动检查目标路径下是否真的存在
bird.jpeg,注意文件名大小写(部分系统区分大小写,Bird.jpeg和bird.jpeg会被识别为不同文件)。
- 手动检查目标路径下是否真的存在
- 修正Multipart参数格式
- Karate中
multipart file的Content-type键名需改为contentType(小写开头,无连字符),修正后代码:* multipart file file = { read: 'bird.jpeg', filename: 'bird.jpeg', contentType: 'image/jpeg' }
- Karate中
- 调整GraphQL上传请求结构
- GraphQL文件上传需遵循
multipart/form-data规范,需将查询语句、变量映射与文件分开传递,示例代码:* def query = read(gqlBase +'updateProfilePicture.gql') * def operations = { query: '#(query)', variables: { file: null } } * def map = { "0": ["variables.file"] } * multipart field operations = operations * multipart field map = map * multipart file 0 = { read: 'bird.jpeg', filename: 'bird.jpeg', contentType: 'image/jpeg' }
- GraphQL文件上传需遵循
内容的提问来源于stack exchange,提问作者Dhamotharan R
相关产品推荐
相关产品推荐

