How would I make POST requests to my Node.js API?

Hello there,

I am making an API where users request to verify their Roblox accounts on Discord, and to complete the verification, they must join a game to complete it.

I already have the game down, just how would I make POST requests and get them from my API? I get the following error when I try to: HTTP 404 (Not Found)

Here is my API code so far:

const discord = require("discord.js")
const express = require("express")
const app = express()
const client = new discord.Client()

app.listen(3000, () => {
  console.log("listening to port 3000.")
})

app.get("/:guildid", (req, res) => {
  console.log("requested")
  let guildId = req.params.guildid
  if (!client.guilds.cache.get(guildId)) {
    return res.json({"error": "unable to get guild"})
  } else {
    let guild = client.guilds.cache.get(guildId)
    const Role = guild.roles.cache.find(role => role.name == "Nitro Booster");
        const Members = guild.members.cache.filter(member => member.roles.cache.find(role => role == Role)).map(member => member.user.tag);
    return res.json(Members)
  }
})

My POST code:

local http = game:GetService("HttpService")

http:PostAsync("https://warmheartedsunnyflashdrive.unusualstudios.repl.co/805141926512558100", http:JSONEncode({name = "Script_Development"}))

Anything will be appreciated! :smiley:

You are trying to make POST requests when the route you assigned is a GET route.

How would I make it POST compatible?

Change app.get to app.post in your route.

When I do this, does it block GET requests?

Yes, but you can always make the same route with app.get.

Alright, let me try this! Thanks.

It doesn’t “block” them, but if you change it to app.post instead of app.get your server will only accept POST requests sent, and then if you attempt to send GET requests it will say “404 not found”.

For example:

const app = express();

app.get("/test-get", (req, res) => {
    res.end("GET request to /test-get works!");
});

app.post("/test-post", (req, res) => {
    res.end("POST request to /test-post works!");
});

With this, if you send a GET request to [address]/test-get (for example http://localhost/test-get), you will be met with the “GET request to /test-get works!”, but if you tried to send a POST request to the same address, it wouldn’t work, because your app simply isn’t accepting that HTTP method on that route.

However, your app does accept POST requests to the [address]/test-post endpoint, and you would be met with the “POST request to /test-post works!” message. Again, if you were to send a GET request to this endpoint it would not work - and would respond with a 404 not found response.

1 Like

One question; how would I get the incoming data?

Which type of data are you sending?

You need the body-parser npm module which you can install using npm i body-parser, and then add the following piece to your code:

const bodyParser = require("body-parser");

app.use(bodyParser.json());

app.post("/post-endpoint", (req, res) => {
    // Assuming you send the data: '{"name": "Script_Development"}'
    console.log(req.body.name) // -> "Script_Development"
});
1 Like

A JSON table. It is shown below:

Oh thanks, I didn’t realize you sent this!