1

I'm a student, and I'm looking for help calling Jdoodle's compiler API (docs here), please.

When I make a post request through Postman and use this as the JSON body, it works...

{
        "script": "console.log('hello world')",
        "stdin": "",
        "language": "nodejs",
        "versionIndex": "0",
        "clientId": "a3462eccc82ecc57a745a23e52c5c71e",
        "clientSecret": "another long string"
}

...and I get the output I expect:

{
    "output": "hello world\n",
    "statusCode": 200,
    "memory": "22764",
    "cpuTime": "0.05"
}

However, I can't get it to work from my Python Flask back-end. Here is my code:

@app_bp.route("/compile", methods=["POST"])
def compile():
    path = "https://api.jdoodle.com/v1/execute"

    query_params = {
        "script": "console.log('hello world')",
        "stdin": "",
        "language": "nodejs",
        "versionIndex": "0",
        "clientId": "a3462eccc82ecc57a745a23e52c5c71e",
        "clientSecret": "another long string here similar to one above"
    }

    response = requests.post(path, params=query_params)
    return response.json()

I've also tried this by passing in headers (headers = {"Content-Type" : "application/json"}), and I still get the same response from Postman:

{
"error": "Invalid Request",
"statusCode": 400 
}

I'm a newbie, and any help would be greatly appreciated. Thank you!

davidism
  • 121,510
  • 29
  • 395
  • 339
  • Are you sure your request body should be passed as query params? You could pass it as form encoded data. https://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests – brandonbanks Jan 03 '22 at 19:48
  • Thanks for your response! I just tried this, and it gave me a different error: 415 Unsupported Media Type. When I add `headers = {"Content-Type" : "application/json"}`, I get the StatusCode 400 again. – user17825364 Jan 03 '22 at 20:42
  • Edit: thanks again! Based on the link you sent me, I was able to try something that worked. importing json and doing this worked out for me... `response = requests.post(url=path, headers = {"Content-Type" : "application/json"}, data=json.dumps(payload))` – user17825364 Jan 03 '22 at 20:45
  • Great! Glad that link was helpful. Good luck in school sir! – brandonbanks Jan 03 '22 at 21:10

0 Answers0