Request answer good only if i use fiddler python

72 Views Asked by At

I work with one project about text ocr

my code on python:

import requests

url = "https://translate.yandex.net/ocr/v1.1/recognize?srv=tr-image&sid=&lang=ru&rotate=auto&yu=&yum=&sprvk="
headers = {
    "Connection": "keep-alive",
    "sec-ch-ua": '"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"',
    "sec-ch-ua-platform": "Windows",
    "sec-ch-ua-mobile": "?0",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
    "Accept": "*/*",
    "Origin": "https://translate.yandex.ru",
    "Sec-Fetch-Site": "cross-site",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Dest": "empty",
    "Referer": "https://translate.yandex.ru/ocr",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
}

files = {
    'fieldNameHere': ('da.webp', open('da.webp', 'rb'), 'image/webp')
}

response = requests.post(url, headers=headers, files=files,verify=False)
print(response.text)

What is wrong - when i just run app without any mitm proxy or sniff apps i get nothing in answer enter image description here

But, when i run fiddler (app to sniff http/https requests) and run code, i get good answer with all data what i need enter image description here

What it can be?

i try use different headers, but it also without results

1

There are 1 best solutions below

2
Andrej Kesely On

You can try to suppress the security warning on the urllib3 level. Also, don't send any headers=:

import requests
from urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)

url = "https://translate.yandex.net/ocr/v1.1/recognize?srv=tr-image&sid=&lang=ru&rotate=auto&yu=&yum=&sprvk="

files = {
    "fieldNameHere": ("russian_text.png", open("russian_text.png", "rb"), "image/png")
}

response = requests.post(url, files=files, verify=False)
print(response.json())

Prints:

{
    "status": "success",
    "data": {
        "detected_lang": "ru",
        "rotate": 0,
        "blocks": [
            {
                "angle": 0,
                "x": 0,
                "y": 45,
                "w": 686,
                "h": 42,
                "rx": 0,
                "ry": 44,
                "rw": 686,
                "rh": 47,
                "boxes": [

...

Image used:

enter image description here