DEV Community

Cover image for Writing Simple RestAPI in Nim With HappyX #3
Ethosa
Ethosa

Posted on

3

Writing Simple RestAPI in Nim With HappyX #3

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
Enter fullscreen mode Exit fullscreen mode

After this let's create post addition method 🍍

  # on POST HTTP method at 127.0.0.1:5000/post/
  post "/post/[postData:AddPost]":
Enter fullscreen mode Exit fullscreen mode

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
    })
Enter fullscreen mode Exit fullscreen mode

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
    }}
Enter fullscreen mode Exit fullscreen mode

You can test it into postman ✌
URL: http://localhost:5000/post/
Method: POST
JSON raw body:

{"title": "Your title", "text": "your text"}
Enter fullscreen mode Exit fullscreen mode

And try to open http://localhost:5000/posts after adding post πŸ‘€

That's all! Good luck ✨

Source code

Image of Timescale

πŸš€ pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applicationsβ€”without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post β†’

Top comments (2)

Collapse
 
tinkerbaj profile image
tinkerbaj β€’

Do you have discord server?

Collapse
 
ethosa profile image
Ethosa β€’

nope, only discord profile

Image of Docusign

πŸ› οΈ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more