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?
Anke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.