Help converting Python code to JavaScript | Sololearn: Learn to code for FREE!


Help converting Python code to JavaScript

Hey, there! I am trying to solve this for some time, yet found no solution till now. (I am still a noob in js :( It would really be a great help to me if someone could convert the below python code to JavaScript that can be used in the Sololearn Web Playground: import requests payload = {'sections':[{'name':'Section1', 'syntax':'autodetect','contents':'name'}]} headers = {'X-Auth-Token': 'token'} post_response ='', json=payload, headers=headers) (Using fetch() is preferred)

3/22/2021 7:55:15 PM


7 Answers

New Answer


Ishmam Farhan solved!! I hope don't arrive too late! Try to add header "Content-Type" with value "application/json". Also it's good send "Accept" with same value. * Accept: tells to server you are expecting in response that mimetype * Content-Type: configure request that tell server that your contentbody is of that mimetype In you inspect the code of python lib you are using, you realize that when pass "json" parameter, it made the work for you. - - Also, in API documentation there an advice mentioning it because accepts two ways of sending body: as json or encoded url


Here do you have all info with some examples how to proceed with post requests Easy... read and extrapolate to your python code


Ishmam Farhan let me a time to deep in how to code But at a glance I see 401 as response status. Reading api docs 401 Unauthorized – Your Application/User application key is wrong.


David Ordás Looks like, now I am the one who arrived late. 😅 Anyways, Thanks so much for the amazing answer!


you should complete the js course first, then you could look at how fetch works... in web context: it may be less restrictive to do that from server side, with nodejs for example...


David Ordás Hi! I have tried my best to follow this article and came up with the below code, but it is only returning a 400 Bad Request response: There might also be some typo I left unnoticed. Anyways, let me know if you can make it work. (You can ping me in the ib to get a dev key)


David Ordás Yep, I got the the Developer key. Didn’t write in the code for privacy reasons. You can always get one if you want. (through ib)