Calling a WebService (written in Python with FastAPI) from Jython does not work

I have written a simple WebService in Python (3.11.2) with “FastAPI” and “pydantic” and need to call it from a Jython application.
Calling the webservice from my Jython application I always get the answer:

{u'detail': [{u'msg': u'Field required', u'loc': [u'body'], u'type': u'missing', u'input': None}]}

That sounds like to me: The body is not sent, no input arrives at the WebService.

My WebService in Python with fastapi and pydantic:

# coding=utf-8

import json
import sys

from typing import Annotated
from fastapi import FastAPI, Body
from pydantic import BaseModel

class InputParams(BaseModel):
    var1: str
    var2: str

def get_function_name():
    return sys._getframe(1).f_code.co_name

app = FastAPI()

@app.post("/post_pydantic/")
async def calculate_not_annotated(inputs: InputParams):
    print()
    print(get_function_name())

    result = {
        "function_name":get_function_name(),
        "var1":inputs.var1,
        "var2":inputs.var2,
    }

    result_json = json.dumps(result)
    return result_json

@app.post("/post_pydantic_annotated/")
async def calculate_annotated(
        inputs: Annotated[
            InputParams,
            Body(
                examples=[
                    {
                        "var1": "first value",
                        "var2": "second value"
                    }
                ]
            )
        ]
):

    print()
    print(get_function_name())

    result = {
        "function_name":get_function_name(),
        "var1":inputs.var1,
        "var2":inputs.var2
    }

    result_json = json.dumps(result)
    return result_json

# ======================================================================================================================

I can start the WebService locally and execute the services from the test platform provided by FastAPI without any problems. Each service produces the expected result.

My Jython application:

# coding=utf-8

from java.net import URI
from java.net.http import HttpClient, HttpRequest, HttpResponse
import json
import sys

def get_function_name():
    return sys._getframe(1).f_code.co_name

def call_httprequest_local(var1, var2):
    print()
    print(get_function_name())


    def createPostRequestWithPydantic(url, var1, var2):
        print()
        print(get_function_name()+": "+url)

        d = {'var1':var1,'var2':var2}
        data_json = json.dumps(d)
        print("Input Dataset JSON: "+str(data_json))

        request = (
            HttpRequest.newBuilder()
            .uri(URI.create(url))
            .POST(HttpRequest.BodyPublishers.ofString(data_json))
            .header("Content-type", "application/json")
            .build()
        )

        client = HttpClient.newHttpClient()
        response = client.send(request, HttpResponse.BodyHandlers.ofString())
        response_raw_json = response.body()

        parsed_response = json.loads(response_raw_json)
        print("response json: "+str(parsed_response))

        return parsed_response

    url = "http://127.0.0.1:8000/"

    result1 = createPostRequestWithPydantic(url=url+"post_pydantic/",var1=var1,var2=var2)
    result2 = createPostRequestWithPydantic(url=url+"post_pydantic_annotated/",var1=var1, var2=var2)

    return




if (__name__) == '__main__':
    call_httprequest_local("hello","world")

I expect the following answers:

call_httprequest_local

createPostRequestWithPydantic: http://127.0.0.1:8000/post_pydantic/
Input Dataset JSON: {"var1": "hello", "var2": "world"}
response json: {"function_name": "calculate_not_annotated", "var1": "hello", "var2": "world"}

createPostRequestWithPydantic: http://127.0.0.1:8000/post_pydantic_annotated/
Input Dataset JSON: {"var1": "hello", "var2": "world"}
response json: {"function_name": "calculate_annotated", "var1": "hello", "var2": "world"}

To test the WebService from another own application, I have written a Python application with “requests”. This allows me to call my WebService without any problems and get the expected result.

Here the python application:

# coding=utf-8

import json
import sys
import requests

def get_function_name():
    return sys._getframe(1).f_code.co_name

def call_httprequest_local(var1, var2):
    print()
    print(get_function_name())

    def createPostRequestWithPydantic(url, var1, var2):
        print()
        print(get_function_name()+": "+url)

        d = {'var1':var1,'var2':var2}
        data_json = json.dumps(d)
        print("Input Dataset JSON: "+str(data_json))

        headers = {"Content-Type":"application/json"}
        request = requests.post(url,data=data_json,headers=headers)
        print("request json: "+str(request.json()))
        return request.json()

    url = "http://127.0.0.1:8000/"

    result1 = createPostRequestWithPydantic(url=url+"post_pydantic/",var1=var1,var2=var2)
    result2 = createPostRequestWithPydantic(url=url+"post_pydantic_annotated/",var1=var1, var2=var2)

    return




if (__name__) == '__main__':
    call_httprequest_local("hello","world")

The python application works, the jython not.

Both applications (Python and Jython) are running on the same computer with Win10 version 22H2. The WebService is also running there.

Does FastAPI not work together with Jython, or where is my error?

To help me, I have now converted the WebService from FastAPI to flask. This means that access with Jython also works.

Nevertheless, I would prefer FastAPI.

Can anyone help me?

New contributor

Anke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật