简体   繁体   中英

API Development detailed endpoints with more requests

I'm using express with node to create endpoints really easily. I have an endpoint called /users/:uid/upload with a POST request. I want it to handle a massive amount of data, however sending the request with about 3.5MB of JSON gets me a 412 error (Payload too large).

My Payload contains an array of (music) artists and its albums, so artist/:artistid/albums/:albumid

So I thought about alternatives:

  1. Alternative: Detailed endpoints with more requests

So I would create the following endpoints:

POST /artists/:artistid/data
POST /artists/:artistid/albums/:albumid/

This would make the large payload become smaller, but then I'd have number_of_artists * number_of_albums_per_artist requests relatively close to each other

  1. Alternative: Uploading JSON as a file

I'd need to persist my data to a file and then upload it, then parse the file and store it somewhere. I'd rather not want this option as the files would need to be updated quite often.

Is there any alternative that I'm missing and would you recommend alternative 1 with many requests?

Thank you

For your current situation you can just increase the size limit.

Install and require body-parser :

const bodyParser = require('body-parser')

Before you declare your endpoints add this:

app.use(bodyParser.json({ limit: '10mb'}))


Although I'd recommend the multiple-requests to detailed endpoints since if one fails you can retry only that specific one. The entire operation doesn't fail that way. So if you got time to invest in this, the best way would be multiple endpoints.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM