Issue with sending POST request using Python requests library

31 views Asked by At

I'm trying to make a POST request to a certain API using Python's requests library. When I send the same request using curl from the command line, it works as expected. However, when I try to replicate the request in Python, it doesn't seem to work correctly.

Here's the curl command that works: curl -X POST -H "Content-Type: application/json" -H "Authorization: Api-Key apikey" -H "x-data-logging-enabled: true" -d "@body.json" https://ocr.api.cloud.yandex.net/ocr/v1/recognizeText -o output.json

Here's the content of body.json:

{
  "mimeType": "JPEG",
  "languageCodes": ["*"],
  "model": "page",
  "content": "/9j/4AAQSkZJRgABAQEBLAEsAAD......"
}

where "content" is a base64 representation of an image. It creates proper output.json with the right response.

And here's my Python code:

import base64
import requests
import json
import urllib3

url = "https://ocr.api.cloud.yandex.net/ocr/v1/recognizeText"


def do_ocr_request(image_path, key):
    file = open(image_path, 'rb')
    encoded_string = base64.b64encode(file.read())
    content = str(encoded_string)[2:-1]

    cur_data = {
        "mimeType": "JPEG",
        "languageCodes": ["*"],
        "model": "page",
        "content": content
    }
    try:
        ocr_result = requests.post(url, data=cur_data, headers={"Content-Type": "application/json",
                                                                "Authorization": "Api-Key {}".format(key),
                                                                "x-data-logging-enabled": "true"})
    except Exception as ex:
        print("Exception during request! {}".format(str(ex)))
        return None
    if ocr_result.status_code != 200:
        print("Daemon return code {}".format(ocr_result.status_code))
        print(ocr_result.content)
        return None
    else:
        return json.loads(ocr_result.content)

Here is how I use the function above.

if __name__ == '__main__':
    key = 'apikey'

    ocr_result = do_ocr_request('test.jpg', key)

And here is what I get:

Daemon return code 400
b'{"error":"invalid character \'m\' looking for beginning of value","code":3,"message":"invalid character \'m\' looking for beginning of value","details":[]}'

The value of "content" fields in body.json and python program are similar.

What's the problem?

0

There are 0 answers