Web Dev Module - doPost

Have an application doing a httpPost to Ignition gateway.
The post comes through fine, as I am able to see the ‘doPost’ script execute.
I have also tried request[‘headers’] and I get the headers just fine.

But when I do a request[‘postData’] , the script errors out with a :

""Traceback (most recent call last): File “”, line 8, in doPost KeyError: postData “”

w/ this code:

‘’’ data = request[‘postData’] ‘’’

The content-type is application/j-son.

Any help would be appreciated!!

Is there really a dash in your content type? If so, take it out.

Well I was just saying what the application is posting. The script actually makes no reference to the content-type. I was just mentioning it because it could be relevant information.

The ‘data = request[‘postData’]’ is the code that is failing

Chris

Your post mentioned the content type is ‘application/j-son’ (with a - in the j-son part).

The Web Dev module should parse the contents of the post into postData only if the header matches one of the following:

application/json (without a dash)
text/*

If the headers are anything else, the data won’t show up in postData, and that line of code you mentioned will fail, because postData won’t exist.

I see what you are saying.

This is the header response I get

‘{‘Accept’: u’application/json, text/plain, /’, ‘User-Agent’: u’axios/0.19.0’, ‘Connection’: u’close’, ‘Host’: u’XX.XX.XX.XX:8088’, ‘Content-Length’: u’408’, ‘Content-Type’: u’application/json;charset=UTF-8’}’

with the exception of the XX’s in the host.

Chris

Yeah, looks like the Content-Type is

application/json;charset=UTF-8

not

application/json

See if you can get the request Content-Type header to be application/json (without the ;charset part).

If those headers can’t be adjusted, you’ll probably need to use the servletRequest object to access the bytes of the post data. (See the Ignition user manual for links to the docs around that object.)

Are you referring to the documentation linked here: HttpServletRequest (Java EE 6 )?

As I do not see reference in the SDK guide.

Thanks for your help you two!

FYI, voted for you at ICC2019 :wink:

Minor note: fixed in 8.0.7 :slight_smile:

Does data = request["data"] work? There’s less special handling for the generic data parameter.

Does not like request[“data”] either :frowning:

from org.apache.commons.io import IOUtils
from java.lang import String

body = IOUtils.toByteArray(request["servletRequest"].inputStream)
jsonString = String(body, "UTF-8")

data = system.util.jsonDecode(jsonString)

Try this - manually retrieving the request data and parsing it into a JSON string.

object has no attribute ‘inputStream’ ??

Whoops. request["servletRequest"].inputStream.

Success.
Thank you very much. :slight_smile: