Sunday, December 14, 2025

Python HTTPX Request Example for MTUOC server


import httpx
headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
}
# Data is string
data = '{"id": "5a521b39-24b6-4fe3-b91d-1ec2b2bbe36f", "src": "Das ist ein Text",
    "srcLang": "de-DE",  "tgtLang": "ro-RO"}'
with httpx.Client(http2=True) as client:
    response = client.post('http://localhost:3000/translate', data=data, headers=headers)
    print(response.json(), response.headers)

import json
json_data = json.loads(data)
with httpx.Client(http2=True) as client:
    response = client.post('http://localhost:3000/translate', json=json_data)
    print(response.text, response.headers)

# Data is dict/json
data = {"id": "5a521b39-24b6-4fe3-b91d-1ec2b2bbe36f", "src": "Das ist ein Text",
    "srcLang": "de-DE",  "tgtLang": "ro-RO"}
with httpx.Client(http2=True) as client:
    response = client.post('http://localhost:3000/translate', json=data)
    print(response.json()['tgt'])