如何用金字塔读取http post参数?

How to read the http post parameters with pyramid?

如何用pyramid读取httppost参数? 如果参数涉及 json 的一部分,我知道该怎么做,但是如果它涉及一个简单的键=值对,我应该如何阅读它? http post 请求示例(使用 hurl.it 进行测试):

Accept: */*
Accept-Encoding: gzip, deflate
Content-Length: 16
Content-Type: application/x-www-form-urlencoded
Host: test.bydehand.com
User-Agent: runscope/0.1

id=tr_uH4yPGBahB 

如果我在代码中执行 print 'request.json_body",它会抱怨,可能是因为它不是有效的 json:

  File "/home/develop/app/daisy/payment/payment_view.py", line 88, in payment_webhook
    logger.debug("Receiving a webhook payment request body: [%s]", str(request.json_body))
  File "/home/develop/env/lib/python3.4/site-packages/pyramid/request.py", line 237, in json_body
    return json.loads(text_(self.body, self.charset))
  File "/usr/lib64/python3.4/json/__init__.py", line 318, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python3.4/json/decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib64/python3.4/json/decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None

如何读取这些 key/value 对并将它们放入字典中?

request.body 包含原始正文。 request.POST 是一个包含解析值的 MultiDict

在您的情况下,request.POST['id'] 将包含该值。

如果您不确定该值是否存在或它是否可能存在多次,您可以使用 request.POST.get('id')request.POST.getall('id')