I have this working API in python, How can I re-build the same thing in javascript? if it's not possible, how can I send this POST request from the font-end?
import requests
gfg_compiler_api_endpoint = "https://ide.geeksforgeeks.org/main.php"
languages = ['C', 'Cpp', 'Cpp14', 'Java', 'Python', 'Python3', 'Scala', 'Php', 'Perl', 'Csharp']
def gfg_compile(lang, code, _input=None, save=False):
data = {
'lang': lang,
'code': code,
'input': _input,
'save': save
}
r = requests.post(gfg_compiler_api_endpoint, data=data)
return r.json()
if __name__ == "__main__":
lang = 'Cpp14'
code = """
#include <iostream>
using namespace std;
int man() {
it a, b;
cin >> a >> b;
cout << (a b);
return 0;
}
"""
_input = "1 5"
print(gfg_compile(lang, code, _input))
CodePudding user response:
You can use a simple xhr request.
var url = "https://example.com/user";
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log(xhr.status);
console.log(xhr.responseText);
}};
var data = `{
"Id": 78912,
"Customer": "Jason Sweet",
"Quantity": 1,
"Price": 18.00
}`;
xhr.send(data);
CodePudding user response:
You can use the 'axios' library. For example,
axios.post('https:localhost:300/page', {
var1: 'abcd',
var2: '1234'
})
.then(function (response) {
console.log(response);
})
You may look up their documentation, https://axios-http.com/. There are tons of youtube videos where it is explained thoroughly.