Ok yes this sounds very confusing. But let me explain a bit more. I'm sure my method of sending text/info to my node.js bot is really bad. That's why I'm wondering if somebody can help me. Currently it can only send one piece of information.
This is my roblox code:
script.Parent.Text = math.random(1, 9999) local HttpService = game:GetService("HttpService") local options = { Url = "http://localhost:8462", -- or whatever url. this is mainly for example purposes. Method = "POST", Headers = { ["Content-Type"] = "text/plain" }, Body = script.Parent.Text, } local success, msg = pcall(function() local response = HttpService:RequestAsync(options) if response.Success then print("Status code:", response.StatusCode, response.StatusMessage) print("Response body:\n", response.Body) else print("The request failed:", response.StatusCode, response.StatusMessage) end end) if not success then print(msg) end
And this is my node.js code which receives the info and stores it for a user runs the code.
const express = require('express') const bodyparser = require('body-parser') const prefix = '' const fs = require('fs'); const Discord = require ('discord.js'); const app = express() var port = 8462 app.use(bodyparser.text()) app.post('/', (request, response) => { response.send("Gotten POST request") const code = request.body console.log(code) client.on('message', message =>{ if(!message.content.startsWith(prefix) || message.author.bot) return; const args = message.content.slice(prefix.length).split(/ +/); const command = args.shift().toLowerCase(); // All the commands. if(command === `/testEmbed`){ const CurrentEmbed = new Discord.MessageEmbed() .setColor(0x8721ed) .setTitle(`It worked`) message.channel.send(CurrentEmbed) // message.channel.send(texto) } else if (command == code){ const CurrentEmbed = new Discord.MessageEmbed() .setColor(0x8721ed) .setTitle(`Code recieved.`) .setDescription(`One last thing, is this you? ||${plr}||`) message.channel.send(CurrentEmbed) } }); }) app.listen(port, function(){ console.log(`started server at http://localhost:${port}`) }) const client = new Discord.Client({ presence: { status: 'online', activity: { name: 'new products', type: 'LISTENING' } } }); // client.once('ready', () => { // }); client.login('Obviously not showing you.)
I hope somebody can help me. As it can currently only send the "request.body" Thanks in advance!
I think I figured it out. Your body looks malformed. Body for HTTP requests have a Key
and Value
system. In this case, Text
would be the key and script.parent.Text
is the key. So, try something like this.
body = { ["Text"] = tostring(script.parent.Text)); }
I also recommend to use a RemoteEvent instead of using it in the script. So the code would look something like this.
The LocalScript
script.Parent.Text = math.random(1, 9999) game.ReplicatedStorage.PostText:FireServer(tostring(script.Parent.Text))
The Server Script
Body = { ["Text"] = tostring(script.parent.Text)); data = '' for k, v in pairs(dataFields) do data = data .. ("&%s=%s"):format( HttpService:UrlEncode(k), HttpService:UrlEncode(v) ) end game.ReplicatedStorage.PostText.OnServerEvent:Connect(function(plr, text) local HttpService = game:GetService("HttpService") local options = { Url = "http://URL:8462", -- or whatever url. this is mainly for example purposes. Method = "POST", Headers = { ["Content-Type"] = "text/plain" }, data, Enum.HttpContentType.TextPlain }, } local success, msg = pcall(function() local response = HttpService:RequestAsync(options) if response.Success then print("Status code:", response.StatusCode, response.StatusMessage) print("Response body:\n", response.Body) else print("The request failed:", response.StatusCode, response.StatusMessage) end end) if not success then print(msg) end end)
The Web Server
app.post("/", async (req, res) => { res.status(200).send("Successfully sent request") })