Python 如何将邮件中的整数发送到Tornado';s AsyncHTTPTestCase.fetch()?

Python 如何将邮件中的整数发送到Tornado';s AsyncHTTPTestCase.fetch()?,python,tornado,httpresponse,Python,Tornado,Httpresponse,我正在使用python的Tornado框架测试我的HTTP POST端点。要做到这一点,我使用的方法 当我这样做时,端点将以字符串“1”的形式接收integer\u arg,即使我希望它以整数的形式接收。这是可以理解的,因为urllib.urlencode将其转换为字符串那么如何确保它接收到一个整数? 仅仅取消对urllib.urlencode的调用是行不通的 顺便说一句,当我使用如下所示的裸体curl调用命中同一个端点时,端点正确地接收到integer\u arg作为integer1 curl

我正在使用python的Tornado框架测试我的HTTP POST端点。要做到这一点,我使用的方法

当我这样做时,端点将以字符串
“1”
的形式接收
integer\u arg
,即使我希望它以整数的形式接收。这是可以理解的,因为
urllib.urlencode
将其转换为字符串那么如何确保它接收到一个整数? 仅仅取消对
urllib.urlencode
的调用是行不通的

顺便说一句,当我使用如下所示的裸体curl调用命中同一个端点时,端点正确地接收到
integer\u arg
作为integer
1

curl \
--request POST \
--header "h1: H1" \
--header "h2: H2" \
--header "Content-Type: application/json" \
--data '{
    "integer_arg": 1, 
    "string_arg": "hello"
}' \
"http://localhost:8000/endpoint"

curl
中的主体与
AsyncHTTPClient.fetch
中的主体明显不同。使用python对curl中的数据进行urlencode,只有json。因此,只需使用json.dumps更改urlencode:

import json
from tornado.ioloop import IOLoop
from tornado.httpclient import AsyncHTTPClient
from tornado.gen import coroutine

@coroutine
def main():
    client = AsyncHTTPClient()
    body = json.dumps({
        'integer_arg': 1,
        'string_arg': 'hello'
    })
    yield client.fetch(
        '/endpoint', method='POST', body=body,
         headers={'h1': 'H1',  'h2': 'H2', 'Content-Type': 'application/json'}
    )

ioloop = IOLoop.instance()
ioloop.run_sync(main)
import json
from tornado.ioloop import IOLoop
from tornado.httpclient import AsyncHTTPClient
from tornado.gen import coroutine

@coroutine
def main():
    client = AsyncHTTPClient()
    body = json.dumps({
        'integer_arg': 1,
        'string_arg': 'hello'
    })
    yield client.fetch(
        '/endpoint', method='POST', body=body,
         headers={'h1': 'H1',  'h2': 'H2', 'Content-Type': 'application/json'}
    )

ioloop = IOLoop.instance()
ioloop.run_sync(main)