How to use pastebin API with python?[specific error]

1.5k Views Asked by At

I'm trying to use the pastebin api with docs: python https://pastebin.com/doc_api. Using the urllib library: https://docs.python.org/3/library/urllib.html.

import urllib.request
import urllib.parse

def main():

    def pastebinner():
        site = 'https://pastebin.com/api/api_post.php'
        dev_key = 
        code = "12345678910, test"      
        our_data = urllib.parse.urlencode({"api_dev_key": dev_key, "api_option": "paste", "api_paste_code": code})  
        our_data = our_data.encode()                    
        resp = urllib.request.urlopen(site, our_data)
        print(resp.read())
        
    pastebinner()

if __name__ == "__main__":
    main()

Here's the error i get:

File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 214, in urlopen return opener.open(url, data, timeout) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 523, in open response = meth(req, response) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 632, in http_response response = self.parent.error( File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 561, in error return self._call_chain(*args) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 494, in _call_chain result = func(*args) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1520.0_x64__qbz5n2kfra8p0\lib\urllib\request.py", line 641, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 422: Unprocessable entity

Any ideas regarding the reason for getting this error?

bump: I still have no idea please help. bump2: :v

2

There are 2 best solutions below

0
Edo Akse On

You are using urllib.request.urlopen(site, our_data) which is an HTTP GET (default for anything in urllib). You need to do an HTTP POST instead. Obligatory w3 link

Please note that the code below is untested

import urllib.request
import urllib.parse


def main():

    def pastebinner():
        site = 'https://pastebin.com/api/api_post.php'
        dev_key = 'APIKEYGOESHERE'
        code = "12345678910, test"
        our_data = urllib.parse.urlencode({"api_dev_key": dev_key, "api_option": "paste", "api_paste_code": code})
        our_data = our_data.encode()
        request = urllib.request.Request(site, method='POST')
        resp = urllib.request.urlopen(request, our_data)
        print(resp.read())

    pastebinner()


if __name__ == "__main__":
    main()

The error is very unhelpful. I mean, why not return a teapot response instead?

0
Jon Doe On

leaving this here in case anyone else runs into this issue. Not 100% sure about this, will test later DONT USE URLLIB2 USE httplib2. I believe that will fix your problem.