Search code examples
pythoncurlrequesturllibpycurl

Python convert curl command to urllib.request


I would like to convert my curl to urllib.request command to python, the curl command:

curl -v -i -X POST http://api.textart.io/img2txt.json --form image=@path/to/dir/file.jpg

my code:

import json
from urllib import request, parse

data = parse.urlencode({
    "image": open("path/to/dir/file.jpg", "rb")
}).encode()

req = request.Request("http://api.textart.io/img2txt.json")
req.add_header('User-Agent', 'Mozilla/5.0')
response = json.loads(request.urlopen(req, data).read())
response = json.dumps(response, indent=4)

print(response)

response:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator at
 [email protected] to inform them of the time this error occurred,
 and the actions you performed just before this error.</p>
<p>More information about this error may be available
in the server error log.</p>
</body></html>

whereas with curl that works, help please.


Solution

  • If you would use module requests instead of urllib then you could use portal http://curlconverter.com to convert it. But sometimes it may create code with mistakes.


    If you would use program postman to test web page then it also has function to generate code in different languages - and it should have function to generate for urllib in Python.


    You can also use portal https://httpbin.org (and url httpbin.org/post) to test request in curland python. Portal sends back all data which it gets in request and you can compare what you send in curl and python.


    But I used on Linux local program netcat to simulate server and see raw request.

    nc -l localhost 8080
    

    and then test curl and urllib with http://localhost:8080

    and created this code which should work with api.textart.io

    from urllib import request
    import json
        
    file_data = open("path/image.jpg", "rb").read()
    
    BOUNDARY = b'------------------------360768b014779354'
    
    data = [
        b'--' + BOUNDARY,
        b'Content-Disposition: form-data; name="image"; filename="image.jpg"',
        b'Content-Type: image/jpeg',
        b'',
        file_data,
        b'--' + BOUNDARY + b'--',
        b'',
    ]
    
    data = b'\r\n'.join(data)
    #print(data)
    
    url = "http://api.textart.io/img2txt.json"
    #url = "https://httpbin.org/post"
    #url = 'http://localhost:8080'
    
    req = request.Request(url)
    
    req.add_header("Content-Type", 'multipart/form-data; boundary={}'.format(BOUNDARY.decode())), 
    #req.add_header("Content-Length", str(len(data)))
    #req.add_header("Accept", "*/*")
    
    response = json.loads(request.urlopen(req, data).read())
    response = json.dumps(response, indent=4)
    print(response)