0

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
 webmaster@api.textart.io 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.

  • Does this answer your question? [Upload Image using POST form data in Python-requests](https://stackoverflow.com/questions/29104107/upload-image-using-post-form-data-in-python-requests) – sushanth Oct 29 '21 at 14:16
  • thanks but now I've a new error, I edit my answer –  Oct 29 '21 at 15:11
  • if you would use `requests` then you could use portal https://curlconverter.com/ to convert it. – furas Oct 29 '21 at 15:49
  • you could use address https://httpbin.org/post in `curl` and `python` - it should send back all information which you send to this url - and you could compare results to see differences. – furas Oct 29 '21 at 15:51
  • I think I see one problem - in Python you send `GET` instead of `POST` – furas Oct 29 '21 at 15:51
  • That works with curlconverter.com thanks! –  Oct 29 '21 at 15:53
  • https://curlconverter.com/ send post –  Oct 29 '21 at 15:54

1 Answers1

0

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)
furas
  • 134,197
  • 12
  • 106
  • 148