W
Wasp-lang•6mo ago
albert6232

crud get

Can seem to figure out how to curl a /Tasks/get to my database. See the following setup: main.wasp
route RootRoute { path: "/", to: MainPage }
page MainPage {
component: import { MainPage } from "@src/MainPage"
}

route HelloRoute { path: "/hello/:name", to: HelloPage }
page HelloPage {
component: import { HelloPage } from "@src/HelloPage"
}

route TestRoute { path: "/test", to: TestPage }
page TestPage {
component: import { TestPage } from "@src/TestPage"
}

entity Task {=psl
id Int @id @default(autoincrement())
description String
isDone Boolean @default(false)
psl=}


crud Tasks {
entity: Task,
operations: {
getAll: {
isPublic: true
},
get: {
isPublic: true
}
}
}
route RootRoute { path: "/", to: MainPage }
page MainPage {
component: import { MainPage } from "@src/MainPage"
}

route HelloRoute { path: "/hello/:name", to: HelloPage }
page HelloPage {
component: import { HelloPage } from "@src/HelloPage"
}

route TestRoute { path: "/test", to: TestPage }
page TestPage {
component: import { TestPage } from "@src/TestPage"
}

entity Task {=psl
id Int @id @default(autoincrement())
description String
isDone Boolean @default(false)
psl=}


crud Tasks {
entity: Task,
operations: {
getAll: {
isPublic: true
},
get: {
isPublic: true
}
}
}
This curl command works on /Tasks/get-all...
curl -X POST http://192.168.0.8:3001/Tasks/get-all
curl -X POST http://192.168.0.8:3001/Tasks/get-all
response is:
{
"json": [
{
"id": 1,
"description": "This is a test",
"isDone": true
},
{
"id": 2,
"description": "This is another test",
"isDone": false
},
{
"id": 3,
"description": "This is one more",
"isDone": false
}
]
}
{
"json": [
{
"id": 1,
"description": "This is a test",
"isDone": true
},
{
"id": 2,
"description": "This is another test",
"isDone": false
},
{
"id": 3,
"description": "This is one more",
"isDone": false
}
]
}
How would I format a curl POST command to /Tasks/get id 1??
10 Replies
kapa.ai
kapa.ai•6mo ago
Hi @albert6232. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To make a POST request to the /Tasks/get endpoint, you would need to include the id of the task you want to retrieve in the body of the request. The id should be sent as a JSON object. Here's how you can format the curl command:
curl -X POST -H "Content-Type: application/json" -d '{"id": 1}' <http://192.168.0.8:3001/Tasks/get>
curl -X POST -H "Content-Type: application/json" -d '{"id": 1}' <http://192.168.0.8:3001/Tasks/get>
This command sets the request method to POST with -X POST, sets the Content-Type header to application/json with -H "Content-Type: application/json", and includes the data {"id": 1} with -d '{"id": 1}'. Please note that the actual endpoint and the way you should structure the request might vary depending on your specific setup. The information provided is based on the general way of making POST requests with curl and the typical structure of CRUD operations in Wasp as described in the Wasp documentation (https://wasp-lang.dev/docs/data-model/crud#declaring-a-crud-with-default-options). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @albert6232, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
albert6232
albert6232OP•6mo ago
@kapa.ai That post command does NOT work...I have tried many various others also.... /get?id=1 /get/1 ... it /get implemented in the CRUD functionality yet? Doc say its BETA
kapa.ai
kapa.ai•6mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- I'm sorry for the confusion. The Wasp framework is indeed in the beta stage and some features might not work as expected. The Wasp documentation (https://wasp-lang.dev/docs/data-model/crud#declaring-a-crud-with-default-options) does mention the get operation as part of the CRUD functionality, which should return one entity based on the id field. However, if you're having trouble with it, it might be a good idea to check the Wasp GitHub issues (https://github.com/wasp-lang/wasp/issues) to see if this is a known problem or to report a new issue. The Wasp team is actively developing and improving the framework, and they might be able to provide more specific help or fixes. In the meantime, you could try using the getAll operation and then filter the results on the client side to get the task with the specific id. I understand this is not the ideal solution, but it might serve as a workaround until the issue with the get operation is resolved. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @albert6232, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
albert6232
albert6232OP•6mo ago
@kapa.ai I have implemented the get-all function and filtered on the client side but would not want that going into production.
kapa.ai
kapa.ai•6mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- I understand your concern. Fetching all data and then filtering on the client side is not an efficient solution for production, especially when dealing with large amounts of data. As of now, the Wasp documentation does not provide a clear example of how to use the get operation in the CRUD functionality. It might be that this feature is not fully implemented or there might be a bug. I recommend reaching out to the Wasp community for more specific guidance. You can report this issue on the Wasp GitHub page (https://github.com/wasp-lang/wasp/issues) or ask for help in the Wasp Discord community (https://discord.com/invite/rzdnErX). The Wasp team and community are usually very responsive and might be able to provide a solution or workaround. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @albert6232, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
martinsos
martinsos•6mo ago
@albert6232 we haven't yet documented how to use it with API, since it is a bit of an experimental feature indeed! How is it that you need to use it via curl? As for how to do it right now, I will ping @miho , he will know the best, but I would guess that the answer Kapa have about providing a JSON in the body would be in the right direciton, if not the solution.
albert6232
albert6232OP•6mo ago
@martinsos thanks for the response ... I did put it in the body as {"Id":1} with no luck... I use the API in other dashboard type internal apps
MEE6
MEE6•6mo ago
Wohooo @albert6232, you just became a Waspeteer level 1!
miho
miho•6mo ago
Few things to note: 1. Directly accessing the operations or CRUD operations is not documented yet, so you'll need to work with our "internal protocol" i.e. Superjson (Wasp uses it to successfully serialize Dates and other types not supported directly in JSON) 2. Luckily, Superjson is simple to use, but it requires a special JSON data shape. Okay, so what do you need to do to get this working? 1. Wrap the JSON you intend to send with { "json": <my-json> } 2. Set the request type to JSON in cURL
curl -X POST http://localhost:3001/Tasks/get -d '{"json":{"id":1}}' -H "Content-Type: application/json"
curl -X POST http://localhost:3001/Tasks/get -d '{"json":{"id":1}}' -H "Content-Type: application/json"
Notice the '{"json":{"id":1}}' payload and the header Content-Type: application/json. This works for me 🙂
albert6232
albert6232OP•6mo ago
perfect!! got it working... thank you.
Want results from more Discord servers?
Add your server