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)
7 Answers
+ 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
+ 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
+ 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.
+ 1
David OrdĂĄs Looks like now I am the one who arrived late. đ
Anyways, Thanks so much for the amazing answer!
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...
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)
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)