#calling GET, POST, DELETE, and PUT request in one next.js api file

27 messages · Page 1 of 1 (latest)

elfin widget
#

Hi guys is it possible to call all 4 CRUD methods in one Next.js api file?

stable cliff
#

hiii. I've already done that. I only can do GET and DELETE. methods for POST and PUT haven't implemented yet.

#

if you want to POST or UPDATE. you can use GET for a while and pass the data in url params

gilded cargo
#

they were implemented at the same time as GET and DELETE

stable cliff
gilded cargo
#

you're simply using it wrong

#

please do not spread misinformation

civic iron
#

If by that you mean, having one API file with all your calls I think you'd have to make calls to that api based on some conditions

#

and I'm not sure it'd be great to have all your API calls in only one file

cedar spire
#

You can have all 4 HTTP request methods inside of one route.tsx file. Make sure to use await request.json() when trying to read the body in the relevant methods

civic iron
#

then I guess he should make a switch

#

and have a special property in the body of the request

#

so he can define which is which

gilded cargo
#

asks a super unclear question
refuses to elaborate
leaves

#

I still can’t understand what exactly OP is trying to do

civic iron
#

I believe he wanted to write 4 calls into one file

#

Like

#

mycalls.ts

fetch1()
fetch2()
fetch3()
fetch4()
stark cargo
#

guys i'm trying to use the delete method, how do i get parameters from the url?

let's say my url looks like this /api/something/id, how do i get the id from it? do i have to use the url object?

civic iron
#

I think you'd be better of to make a call with a body to the API

#

// calltoapi.js

const mydata = {
     "id": 50
}
fetch("myapi", {
     method: "POST",
     headers: {
        "Content-type": "application/json"
   },
      body: JSON.stringify(mydata)
}

// myapi.js

   // api boiler plate code
 const { id } = req.body