Need help with Express api upload via Insomania
See original GitHub issueThanks @jaydenseric for this plugin, I’m working on Express API (mongodb, apollo server off course ) and using Insomania to test all my features.
Here is my code
index.js
const helperMiddleware = [
bodyParser.json(),
bodyParser.urlencoded({ extended: true }),
bodyParser.text({ type: 'application/graphql' }),
(req, res, next) => {
if (req.is('application/graphql')) {
req.body = { query: req.body }
}
next()
}
]
const buildOptions = async (req, res) => {
res.removeHeader("X-Powered-By") // remove x power headers
const mongo = await connectMongo()
const user = await authentication(req, res, mongo.Users)
return {
schema,
context: {
mongo,
dataloaders: buildDataloaders(mongo),
user
},
// tracing: true
}
}
app.use(
'/api',
...helperMiddleware,
apolloUploadExpress({
maxFileSize: 5 * 1000,
maxFiles: 5
}),
graphqlExpress(buildOptions)
)
schema
type Image {
# upload
id: ID!
path: String!
filename: String!
mimetype: String!
encoding: String!
# data
caption: String
created_by: User!
created_date: Datetime
width: Int
height: Int
url: String
album: Album
variants: [ImageVariant]
}
mutation schema
uploadImage(files: [Upload!]!): [Image!]
resolver
const imgDir = './public/uploads'
const storeUpload = async ({ stream, filename }) => {
const id = uuid.v4()
const path = `${imgDir}/${id}-${filename}`
return new Promise((resolve, reject) =>
stream
.pipe(createWriteStream(path))
.on('finish', () => resolve({ id, path }))
.on('error', reject)
)
}
const processUpload = async (upload, Images) => {
const { stream, filename, mimetype, encoding } = await upload
const { id, path } = await storeUpload({ stream, filename })
const newImage = { id, filename, mimetype, encoding, path }
const response = await Images.insert(newImage)
return Object.assign({ id: response.insertedIds[0] }, newImage)
}
export const uploadImage = async (root, data, { mongo: { Images }, user }) => processUpload(data.files, Images)
Insomania Query
mutation Upload($files: [Upload!]!) {
uploadImg(files: $files) {
filename
encoding
mimetype
path
}
}

Expected
- Upload multiple images within maximum 5 files (and maximum 5MB for each)
- Insert data to db
Result
Non-stop sending data request in Insomania
I don’t know what I missed, would you please help me out, I’m kind of newbie.
Thank you.
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (4 by maintainers)
Top Results From Across the Web
Insomnia REST Client Tutorial - YouTube
APIs for Beginners - How to use an API (Full Course / Tutorial) · Salesforce Integration Crash Course | The Ultimate Guide to...
Read more >Insomnia upload pic and post data at same time - Stack Overflow
Insomnia offers only URL Encoded for data and Binary File for file. I cannot send both at the same time. What should I...
Read more >How to Create Documentation for Your REST API with Insomnia
Open the Insomnia app and go to your dashboard. Create a new Design Document by clicking the Create button on the top right...
Read more >Using the Upload API Endpoint – Help Center - Support - Zype
To upload a video using Insomnia, you have to set the body as multipart and add the file and all the other necessary...
Read more >Building an API with Express and Node.js - DEV Community
After we install Insomnia, let's make a GET request to localhost:4000/toys , to make sure our backend application is working correctly and data ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You’re getting closer 😛
The reason that did not work is because the
query
,operationName
andvariables
multipart form fields don’t exist in the spec, and the requiredoperations
field is missing.You might find it useful to study the construction of the requests in the
apollo-upload-server
tests.Most people never have to manually construct requests like this; everything is done for you by
apollo-upload-client
.yes, follow your example and use
apollo-upload-client
is more easier for me to make it work, I’ll learn more about the test case, thanks in advance @jaydenseric