[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: CGI and JSON


Le 10/04/2024 à 20:47, Jesus Guardon a écrit :
Hi all

Currently I'm trying to get running my first web application to build an API Rest Service using JSON to send data back and forth.

I am using Request.Post to retrieve the content of a raw Json POST data with "application/json" header mimetype, but I have no idea how to get the raw content in the request.Post function.

I know how to do it by sending my data as "x-www-form-urlencoded" and it works, but instead I would like to get the entire body of the post response. That is how other devs do it in other languages and seems to be the standard way to deal with JSON data.

When I print CGI.Dump() after my request I can see in the "CONTENT_LENGHT" environment variable the correct lenght of the sent data, but there doesn't seem to be a way to retrieve the actual data.

Benoît, would you shed some light over this?

By the way, I'm using Postman as a tool for crafting the POST requests and it's really a great tool.

https://www.postman.com/downloads/

Thanks in advance


Request.Post only deals with 'application/x-www-form-urlencoded' and 'multipart/form-data" content type.

For other content type (like 'application/json'), you must read it manually from the standard input.

So you do something like that:

--8<----------------------------------------------------

if Request.Method = "POST" And If Request.ContentType = "application/json" Then

  sData = Read Request.ContentLength
  MyJsonData = JSON.FromString(sData)
  ...

Endif

--8<----------------------------------------------------

Regards,

--
Benoît Minisini.


Follow-Ups:
Re: CGI and JSONJesus Guardon <ea7dfh@xxxxxxxxxx>
References:
CGI and JSONJesus Guardon <ea7dfh@xxxxxxxxxx>