FastAPI POST endpoint returning "Field required" when sending dictionary from Jython

Hi everyone,


I'm trying to use a POST method:

h = {"accept": "application/json", "Content-Type": "application/json"}

client = system.net.httpClient()
response = client.post(url, data=data, headers=h)
response.json

data is a dictionary.

I get this error:

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

For context, I’m using FastAPI for my API.

The endpoint is defined like this:

@app.post("/item")
def update_item(new_item: Item):
    ... 

And the Item model looks like this:

class Item(BaseModel):
    t_stamp: str
    temp_1: float
    hum_1: float
    temp_2: float
    hum_2: float
    temp_3: float
    hum_3: float
    temp_4: float
    hum_4: float
    temp_5: float
    hum_5: float
    temp_6: float
    hum_6: float


The perplexing part is that if I use httpPost instead, it works fine:

import json

# Convert dictionary to JSON string
json_string = json.dumps(data)

# Use httpPost directly
response = system.net.httpPost(url, contentType="application/json", postData=json_string)

Thanks in advance for any help or advice

what happens if you try passing in json.dumps(data) to httpClient?

Can you share the actual dictionary you're sending?

What if you send your data to a testing tool like that echoes what you requested back to you?
I like http://httpbin.org/, though it appears to be down at the moment. It's self hostable via Docker: https://hub.docker.com/r/kennethreitz/httpbin/

Having same issue with put -

client = system.net.httpClient()
response = client.put(url, params = {'record_id':1} data={"ValueToUpdate":1.00}, headers=h)
response.json

Why would you expect PUT to have a response body at all?

1 Like

to see if the put was successful or was there any error.

1 Like

This code snippet doesn't have a comma between your two parameters.

yes,

here is my function :

def clientPut( path = '', id = None, payload = {}):
	if id is None:
		return
	headers = {
        "Content-Type": "application/json",
       "accept": "application/json"
        }
        
	client = system.net.httpClient()
	value =  client.put(path, params = {'record_id':id}, data = payload, headers=headers)
	print value.request.headers
	print value.json		
	return value.json

Here is the call:

clientPut(path = URL, id=411, payload = {"maximum":1.57})

this is what it prints when I use script console in designer:

{u'accept': [application/json], u'Content-Type': [application/json], u'User-Agent': [Ignition]}
{'detail': [{'msg': u'Field required', 'loc': [u'body'], 'type': u'missing', 'input': None}]}
{'detail': [{'msg': u'Field required', 'loc': [u'body'], 'type': u'missing', 'input': None}]}

Seems like a server problem to me; it works fine against an echo server:

from pprint import pprint

URL = "https://httpbingo.org/put"

def clientPut( path = '', id = None, payload = {}):
	if id is None:
		return
	headers = {
        "Content-Type": "application/json",
       "accept": "application/json"
        }
        
	client = system.net.httpClient()
	value =  client.put(URL, params = {'record_id':id}, data = payload, headers=headers)
	print value.request.headers
	return value.json
	
pprint(clientPut(path = URL, id=411, payload = {"maximum":1.57}))
>>>
{u'accept': [application/json], u'Content-Type': [application/json], u'User-Agent': [Ignition]}
{'args': {'record_id': [u'411']},
 'data': u'{"maximum":1.57}',
 'files': {},
 'form': {},
 'headers': {'Accept': [u'application/json'],
             'Content-Length': [u'16'],
             'Content-Type': [u'application/json'],
             'Host': [u'httpbingo.org'],
             'User-Agent': [u'Ignition'],
             'Via': [u'2 fly.io'],
             'X-Forwarded-For': [u'207.173.137.144, 66.241.125.232'],
             'X-Forwarded-Port': [u'443'],
             'X-Forwarded-Proto': [u'https'],
             'X-Forwarded-Ssl': [u'on'],
             'X-Request-Start': [u't=1763134710144631']},
 'json': {'maximum': 1.57},
 'method': u'PUT',
 'origin': u'207.173.137.144',
 'url': u'https://httpbingo.org/put?record_id=411'}

I messaged the full trace.

Try this, because some servers are bad at negotiating HTTP_2 downgrades.

Make this call:
client = system.net.httpClient()

Into:
client = system.net.httpClient(version="HTTP_1_1")

1 Like

that did indeed work

1 Like