WebDev-doPost - request from Postman

I have a doPost method in WebDev where i want to simulate getting some data from a PLC. The data will then be written to tags. I'm setting up a request in postman towards the endpoint with some json data. I am wondering which method i should use to get this data within my doPost method

def doPost(request, session):
    # Definer tag-stien for å skrive RFID UID
	logger = system.util.getLogger("GetData_SaveToTag_Project")
	logger.info("jsonPostSimatic start")
	
	tagPath = "[default]RFID_UID"
	timeStamp = "[default]DataArrivalTimestamp"
	dataArrival = "[default]DataArrival"
	
	logger.info("BEFORE DATA: tagPath: {}, timeStamp: {}, dataArrival: {}".format(tagPath, timeStamp, dataArrival))
	
	# Evt: For testing, bruker vi en predefinert json payload istedet for request mot Siemens enheten
	# jsonPayload = '{"UID": "E004015062F0D272", "MAC": "78:C4:0E:A0:7E:26", "Timestamp": "2024-08-15T10:00:00.000+02:00"}'

	
	try:
		# Hent JSON-payloaden fra HTTP POST-forespørselen
		jsonPayload = request.data.decode("utf-8")  # Dekoder bytes til streng
		
		# Dekod JSON-payload til en Python-dictionary
		data = system.util.jsonDecode(jsonPayload)
		logger.info("DATA: {}".format(data))
		
		# Få UID fra JSON-dataene
		uid = data.get("UID", "Ingen UID funnet..")
	
		
		# currentClientIdentifier = session.custom.clientIdentifier
		clientIdentifier = data.get('clientIdentifier', 'UnknownClient')
		timeStamp = data.get('Timestamp', system.date.now()) # bruker timestamp fra JSON eller now() hvis ikke angitt)
		
		logger.info("UID: {}".format(uid))
		logger.info("clientIdentifier: {}, currentTime: {}".format(clientIdentifier, currentTime))
		
		# Skriv UID til Ignition-taggen
		system.tag.writeBlocking([tagPath], [uid])
		
		# Bestem om denne klienten skal vise dataankomst-indikatoren
		if clientIdentifier == "ClientA":
		    # Oppdater UI eller utfør handlinger spesifikke for ClientA
		    system.tag.writeBlocking([dataArrival], [True])
		    
		    # oppdaterer DataArrivalTimestamp
		    system.tag.writeBlocking([timeStamp], [timestamp])
		
		# Returner en suksessrespons
		return {'html': "<html><body>Data mottatt og tagg oppdatert vellykket.</body></html>"}
	
	except Exception as e:
	    # Håndter eventuelle feil som oppstår
	    return {'html': "<html><body>Feil ved behandling av forespørsel: {}</body></html>".format(str(e))}

sorry for the norwegian comments, still working on this
but struggling to figure out what method to use to actually fetch the json data from the request.

as you can see i defined the json payload earlier directly in the script, which worked fine

could i use something like system.net or system.web?

try request["postData"]

2 Likes

That worked. Thanks a lot!