[DONE] Convert Python request code to JavaScript XMLhttp request | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

[DONE] Convert Python request code to JavaScript XMLhttp request

I need help to convert this Python3 code into javascript code. import requests url = 'https://myurl.com/api.php' data = { 'data': 'a', 'type': 'message', 'sent': 'true' } r = requests.post(url, data = data) print(r.json()) Thank you.

10th Nov 2020, 5:39 PM
Bibek Oli
Bibek Oli - avatar
1 Answer
+ 5
The JavaScript code would look like, let url = 'https://myurl.com/api.php' let data = { data: 'a', type: 'message', sent: 'true' }; fetch(url, { method: "POST", body: JSON.stringify(data), headers: { "Content-Type": "application/json" }) .then(res => res.json()) .then(resp => { console.log(resp); });
10th Nov 2020, 5:45 PM
777
777 - avatar