11

I just need to write a simple python CGI script to parse the contents of a POST request containing JSON. This is only test code so that I can test a client application until the actual server is ready (written by someone else).

I can read the cgi.FieldStorage() and dump the keys() but the request body containing the JSON is nowhere to be found.

I can also dump the os.environ() which provides lots of info except that I do not see a variable containing the request body.

Any input appreciated.

Chris

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
Christopher
  • 5,806
  • 7
  • 31
  • 41

2 Answers2

27

If you're using CGI, just read data from stdin:

import sys
data = sys.stdin.read()
ars
  • 120,335
  • 23
  • 147
  • 134
8

notice that if you call cgi.FieldStorage() before in your code, you can't get the body data from stdin, because it just be read once.

vusan
  • 5,221
  • 4
  • 46
  • 81
Zhihao Wang
  • 81
  • 1
  • 1