Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No body in request ! bad request #728

Closed
HaDuyLong-EP opened this issue Dec 24, 2024 · 1 comment
Closed

No body in request ! bad request #728

HaDuyLong-EP opened this issue Dec 24, 2024 · 1 comment
Assignees

Comments

@HaDuyLong-EP
Copy link

Describe the bug
A clear and concise description of what the bug is. I have code in Genie is like this

Import necessary packages

using Genie, Genie.Router, DataFrames, Arrow, Random, HTTP, JSON

Configure Genie

#Genie.config.run_as_server = true

Configure global CORS headers

Genie.config.cors_headers["Access-Control-Allow-Origin"] = "*" # Allow all origins
Genie.config.cors_headers["Access-Control-Allow-Methods"] = "GET, POST" # Allow methods
Genie.config.cors_headers["Access-Control-Allow-Headers"] = "Content-Type, Origin, Accept"

Define the simulation route

route("/api/run_simulation", method = POST) do
try
# Access the current request
req = Genie.Requests.request()
println("Received request: ", req) # Debugging: log request

   # Read and log the raw body
   raw_body = String(req.body)
   println("Received raw body: ", raw_body)

   if isempty(raw_body)
       println("Empty request body received.")
       return HTTP.Response(400, "Bad Request: Empty body")
   end

   # Parse JSON from body
   input_json = JSON.parse(raw_body)
   println("Parsed JSON: ", input_json)

   # Respond with a success message
   response = Dict(
       "message" => "Simulation received successfully",
       "received_data" => input_json
   )
   return HTTP.Response(200, JSON.json(response))

    # Placeholder simulation logic
    result = Dict(
        "data" => [
            Dict("x" => [1, 2, 3], "y" => [4, 5, 6], "type" => "scatter", "name" => "Simulation")
        ],
        "layout" => Dict(
            "title" => "Simulation Results",
            "xaxis" => Dict("title" => "Time"),
            "yaxis" => Dict("title" => "Value")
        )
    )

    # Respond with simulation results
    return HTTP.Response(200, JSON.json(result))
catch e
    println("Error processing request: ", e)
    return HTTP.Response(500, "Internal Server Error")
end

end

Handle OPTIONS preflight requests for CORS

route("/api/run_simulation", method = OPTIONS) do
return HTTP.Response(200, headers = Dict(
"Access-Control-Allow-Origin" => "*",
"Access-Control-Allow-Methods" => "GET, POST, OPTIONS",
"Access-Control-Allow-Headers" => "Content-Type, Origin, Accept"
))
end

Start the Genie server

Genie.up()

Error stacktrace
If any error is thrown, please copy from the REPL and paste it here

To reproduce
Steps to reproduce the behavior and/or Julia code executed.

Expected behavior
A clear and concise description of what you expected to happen.

Additional context
Please include the output of
julia> versioninfo()
and
pkg> st


Then I make a request by Curl : curl -X POST -H "Content-Type: application/json" -d '{"timestep": "1s", "duration": "1d", "start_date": "2021-01-01T00:00"}' http://localhost:8000/api/run_simulation
Bad Request: Empty body(base)

I have Bad request, we don't have json inside at all

curl -X POST -H "Content-Type: application/json" -d '{"timestep": "1s", "duration": "1d", "start_date": "2021-01-01T00:00"}' http://localhost:8000/api/run_simulation
Bad Request: Empty body(base)

Please answer these optional questions to help us understand, prioritise, and assign the issue

1/ Are you using Genie at work or for hobby/personal projects? For work

2/ Can you give us, in a few words, some details about the app you're building with Genie? For simulation the energy market

@HaDuyLong-EP
Copy link
Author

Dear all,

I figure out how to make it work for by pass the cors policy with that code:

Import necessary packages

using Genie, Genie.Router, Genie.Requests, DataFrames, Arrow, Random, HTTP, JSON

Configure Genie

Genie.config.run_as_server = true

Configure global CORS headers

Genie.config.cors_headers["Access-Control-Allow-Origin"] = "" # Allow all origins
Genie.config.cors_headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS" # Allow methods
Genie.config.cors_headers["Access-Control-Allow-Headers"] = "Content-Type, Authorization"
Genie.config.cors_headers["Access-Control-Allow-Credentials"] = "true"
Genie.config.cors_headers["Access-Control-Expose-Headers"] = "
"

Define the simulation route

route("/api/run_simulation", method = POST) do
try

    message = JSON.json(jsonpayload())
    println("Received message: ", message)

   # Placeholder simulation logic
    result = Dict(
        "data" => [
            Dict("x" => [1, 2, 3], "y" => [4, 5, 6], "type" => "scatter", "name" => "Simulation")
        ],
        "layout" => Dict(
            "title" => "Simulation Results",
            "xaxis" => Dict("title" => "Time"),
            "yaxis" => Dict("title" => "Value")
        )
    )

    # Respond with simulation results
    return HTTP.Response(200, JSON.json(result))
catch e
    println("Error processing request: ", e)
    return HTTP.Response(500, "Internal Server Error")
end

end

Handle OPTIONS preflight requests for CORS

route("/api/run_simulation", method = OPTIONS) do
return HTTP.Response(200, "", Dict(
"Access-Control-Allow-Origin" => "",
"Access-Control-Allow-Methods" => "GET, POST, OPTIONS",
"Access-Control-Allow-Headers" => "Content-Type, Authorization",
"Access-Control-Allow-Credentials" => "true",
"Access-Control-Expose-Headers" => "
",
"Access-Control-Max-Age" => "86400"
))
end

Start the Genie server

Genie.up()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants