gqlgen
gqlgen copied to clipboard
Fail to upload file
What happened?
Upgrade from 0.13.0 to 0.17.44 and go from 1.16 to 1.22
What did you expect?
Can upload file previously, but right now throw the following issue
{
"errors": [
{
"message": "failed to parse multipart form"
}
],
"data": null
}
The request body show follow
POST /portal?lang=en_US HTTP/1.1
> Host: localhost:8080
> User-Agent: insomnia/8.6.1
> Content-Type: multipart/form-data; boundary=X-INSOMNIA-BOUNDARY
> Authorization: Bearer xxxxxxxxxx
> Accept: */*
> Content-Length: 3021
| --X-INSOMNIA-BOUNDARY
| Content-Disposition: form-data; name="operations"
| {"query":"mutation ($file: Upload!) .........
| --X-INSOMNIA-BOUNDARY
| Content-Disposition: form-data; name="map"
| {"0":["variables.file"]}
| --X-INSOMNIA-BOUNDARY
| Content-Disposition: form-data; name="0"; filename="logo.svg"
| Content-Type: image/svg+xml
| <svg width="110" height="44" viewBox="0 0 110 44" ............
| --X-INSOMNIA-BOUNDARY--
versions
-
go run github.com/99designs/gqlgen version
? 0.17.44 -
go version
? 1.22
Try different go version with gqlgen version
gqlgen v0.17.33
with go v1.18
=> Not work
gqlgen v0.17.10
with go v1.16
=> Not work
gqlgen v0.17.0
with go v1.16
=> Not work
gqlgen v0.16.0
with go v1.16
=> Work
gqlgen upgrade to v0.17.x will cause upload fail issue
Have you tried apollo upload client?
i did't use apollo as my client. For gqlgen, after 0.17.0
, it assume the client is using apollo client?
Any update for the issue? My request show as below
operations= {"query":"mutation ($file: Upload!) {\n\tupdate_user_profile(input: {\n\t\tfull_name: \"King Wu 321\"\n\t}, profileFile: $file) {\n\t\t_id\n\t\tupdated_at\n\t\tname\n\t\tcreated_at\n\t\tprofile_image {\n\t\t\t_id\n\t\t\turl\n\t\t}\n\t}\n}"}
Finally, found that the middleware handle multipart, which has error
Fixed by the following
// Ensure the form is parsed correctly
if r.Method == http.MethodPost || r.Method == http.MethodPut {
if r.Header.Get("Content-Type") == "multipart/form-data" {
if err := r.ParseMultipartForm(32 << 20); err != nil && err != http.ErrNotMultipart {
http.Error(w, "Failed to parse multipart form", http.StatusBadRequest)
return
}
} else {
r.ParseForm()
}
} else {
r.ParseForm()
}