0

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 = requests.post(url='https://api.paste.ee/v1/pastes', json=payload, headers=headers) (Using fetch() is preferred)

22nd Mar 2021, 7:55 PM
Ishmam
Ishmam - avatar
7 Réponses
+ 2
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. - https://docs.python-requests.org/en/master/api/#requests.Request - https://docs.python-requests.org/en/master/_modules/requests/models/#PreparedRequest.prepare_body Also, in API documentation there an advice mentioning it because accepts two ways of sending body: as json or encoded url https://pastee.github.io/docs/#submit-a-new-paste
31st Mar 2021, 11:48 AM
David Ordás
David Ordás - avatar
+ 1
Here do you have all info with some examples how to proceed with post requests https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch Easy... read and extrapolate to your python code
23rd Mar 2021, 1:46 AM
David Ordás
David Ordás - avatar
+ 1
Ishmam Farhan let me a time to deep in how to code But at a glance I see 401 as response status. Reading paste.ee api docs https://pastee.github.io/docs/#errors 401 Unauthorized – Your Application/User application key is wrong.
23rd Mar 2021, 10:22 AM
David Ordás
David Ordás - avatar
+ 1
David Ordás Looks like now I am the one who arrived late. 😅 Anyways, Thanks so much for the amazing answer!
19th Apr 2021, 6:17 PM
Ishmam
Ishmam - avatar
0
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...
22nd Mar 2021, 8:11 PM
visph
visph - avatar
0
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: https://code.sololearn.com/WyC9y7wJAF9s/?ref=app 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)
23rd Mar 2021, 4:15 AM
Ishmam
Ishmam - avatar
0
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)
24th Mar 2021, 4:22 AM
Ishmam
Ishmam - avatar