0

My code looks like this. I just need to be able to access that object. I come from a javascript background and am used to being able to capture the response object and access the response body and all is well. I am not quite understanding how I am to access that though. I have tried form["key_name"].value etc but still get back nothing. Can someone please help me access my data from FieldStorage(None, None, '{"phrase":"work mudasucka!"}')? I would greatly appreciate it!

 def do_POST(self):
        if self.path=="/send":
            form = cgi.FieldStorage(
                fp=self.rfile, 
                headers=self.headers,
                environ={'REQUEST_METHOD':'POST',
                'CONTENT_TYPE':self.headers['Content-Type'],
                })

            print (form)
            self.send_response(200)
            self.end_headers()
        return

 #This is what prints in my console.
 #FieldStorage(None, None, '{"phrase":"work mudasucka!"}')
str8up7od
  • 338
  • 3
  • 15

1 Answers1

1

The solution was a simple for loop. I looped over the forms keys using the .key() method that form has and was able to extract the value with the .getvalue() method. That method takes a single argument of key. Worked like a charm!

if self.path=='/getOne':
        form = cgi.FieldStorage(
            fp=self.rfile, 
            headers=self.headers,
            environ={'REQUEST_METHOD':'POST',
            'CONTENT_TYPE':self.headers['Content-Type'],
        })

        for key in form.keys():
            value = str(form.getvalue(key))
            t = ('%' + value + '%')

        c.execute('select * from appointments where description like ?' , (t,))
        res = c.fetchall()
        _json = json.dumps(res)
        self.send_response(200)
        self.send_header("Content-type", "application/json")
        self.end_headers()
        self.wfile.write(_json)
        print "I'm ya huckleberry", res
    return
str8up7od
  • 338
  • 3
  • 15