Hi there π!
Previously we add posts getting π.
Now we implement post adding β.
At first go into src/main.nim
and add Request Model
import
# This will import HappyX framework
happyx
# Request model AddPost
# with two string fields
model AddPost:
title: string
text: string
After this let's create post addition method π
# on POST HTTP method at 127.0.0.1:5000/post/
post "/post/[postData:AddPost]":
Here we use AddPost
as postData
immutable var π
Let's add sended post into posts
variable
# on POST HTTP method at 127.0.0.1:5000/post/
post "/post/[postData:AddPost]":
posts.add(%*{
"title": postData.title,
"text": postData.text
})
Here we convert AddPost
model to JsonNode
object and add it into our posts
Let's says user about post successfully added into our posts β¨
Just add return
statement
# on POST HTTP method at 127.0.0.1:5000/post/
post "/post/[postData:AddPost]":
posts.add(%*{
"title": postData.title,
"text": postData.text
})
return {"response": {
"index": posts.len - 1
}}
You can test it into postman β
URL: http://localhost:5000/post/
Method: POST
JSON raw body:
{"title": "Your title", "text": "your text"}
And try to open http://localhost:5000/posts
after adding post π
That's all! Good luck β¨
Top comments (2)
Do you have discord server?
nope, only discord profile