Using post requests to access a server

I’m currently trying to make my script access a server on replit. It can reach it, but always returns an empty dict when the server sees the data.

Script:

local http = game:GetService("HttpService")
local Data = {
	["d"] = "e"
}

Data = http:JSONEncode(Data)

r = http:PostAsync("https://replit-link/ret", Data) --Put the link you saved between the two quotes.
print(r)

My server:

from flask_restful import Resource, Api
from flask import Flask, jsonify
from threading import Thread
from flask import Flask, request, render_template

from PIL import Image, ImageOps
from io import BytesIO

import requests
import time

app = Flask(__name__)
api = Api(app)

@app.route("/",methods=["GET", "POST"])
def home():
  return "Online"


@app.route('/ret', methods=['POST'])
def form_to_json():
    data = request.form.to_dict(flat=False)
    print(data) #tries to print data, gets nothing
    
    return data

class GetFunction(Resource):
  print("Functions loading")

  def get(self, Function : str = None):
    Data = "No data found."

    
    return jsonify(Data) or Data
  
  print("Functions loaded")

def run():
  app.run(host = "0.0.0.0", port = 8080)
def start():
  t = Thread(target = run)
  t.start()

EDIT: It returns {} on the server

1 Like

https://replit-link/ret

Appears to be down currently.

I know how to fix it!!

I spent awhile trying to fix this

Do this for your form_to_json():

@app.route('/ret', methods=['POST'])
def form_to_json():
  print(request.json)
  data = request.json
  print(data) # You should now be able to receive the dict
    
  return data

For an explanation

You need to use request.json to get the json data
NOT request.form.to_dict() else that’ll just return nothing.

Good luck for anyone who needs help with this, I personally love your code @SnippyRO.

Helped me out a lot.

Oh and for Roblox, I created a function:

local function sendRequest(plr)
	local Data = {
		["plr"] = plr.Name
	}
	Data = http:JSONEncode(Data)
	local r = http:PostAsync(IP, Data, Enum.HttpContentType.ApplicationJson)
	print(r)
end

I added a 3rd Argument for PostAsync().
Enum.HttpContentType.ApplicationJson

I’m unsure if you really need it but just thought I’d note that