Forum OpenACS Development: Re: Attempt to use a nonstandard

Collapse
Posted by Gustaf Neumann on
A couple of answers:
  • when using ad_page_contracts, the parameters have to be either query parameter (in the URL) or form parameter (application/x-www-form-urlencoded). In your example, ad_page_contract tries to interpret the JSON data as form parameters. Probably ad_package_contract should be extended to provide a better error messages when receiving POST requests with unexpected content types.
  • in theory ad_page_contract could be extended to handle also JSON data, but that would require a JSON parser, it would be necessary to handle tree-structured data (like query or form parameters), and many possible error condition have to handled, etc.

Therefore, you have to handle the incoming JSON data via custom pages (without page contracts).
The proper way to handle this is to check in your page
- is this a POST request
- if yes, get the body with ns_getcontent [1] and
- parse the content with a JSON parse
- provide custom error messages in case JSON parsing fails.

[ns_conn headers] can be used to obtain the set of the HTTP request header fields, which you can use to obtain the content type of the post request. [ns_conn query] can be used, to obtain the query parameters of the HTTP request. You should not expect the body of an HTTP request returned by these commands.

hope, this helps
-gn

[1] https://naviserver.sourceforge.io/n/naviserver/files/ns_getcontent.html