AIOHTTP:如何为AIOHTTP帖子创建数据表格
这就是我使用请求时的数据的样子,并且可以正常工作。
data ={
"srt": srt,
"firstname" : firstname,
"lastname" : lastname,
"Email" : email,
"password" : password,
"promotion" : "true",
"action" : {"name":"EMAIL_REG_FORM_SUBMIT"},
"ri" : "NORU",
"ets" : ets
}
我正在尝试将其转换,以便它可以与AioHTTP一起使用,这就是我所拥有的。 我会遇到错误
data = aiohttp.FormData()
data.add_field("srt", srt)
data.add_field("firstname", firstname)
data.add_field("lastname", lastname)
data.add_field("Email", email)
data.add_field("password", password)
data.add_field("promotion", 'true')
data.add_field("action", {"name":"EMAIL_REG_FORM_SUBMIT"})
data.add_field("ri", 'NORU')
data.add_field("ets", ets)
我认为由于行:“ action”:{“ name”:“ email_reg_form_submit”},
,如果有人对如何使这项工作有任何想法,请发表评论, 。从本质上讲,如果您知道该怎么做,请告诉我,我需要一个会话中的异步请求。
This is what the data looks like when I'm using requests and it works fine.
data ={
"srt": srt,
"firstname" : firstname,
"lastname" : lastname,
"Email" : email,
"password" : password,
"promotion" : "true",
"action" : {"name":"EMAIL_REG_FORM_SUBMIT"},
"ri" : "NORU",
"ets" : ets
}
I'm trying to convert it so that it works with aiohttp and this is what I have. I think I'm getting an error because of line: "action" : {"name":"EMAIL_REG_FORM_SUBMIT"},
data = aiohttp.FormData()
data.add_field("srt", srt)
data.add_field("firstname", firstname)
data.add_field("lastname", lastname)
data.add_field("Email", email)
data.add_field("password", password)
data.add_field("promotion", 'true')
data.add_field("action", {"name":"EMAIL_REG_FORM_SUBMIT"})
data.add_field("ri", 'NORU')
data.add_field("ets", ets)
If anyone has any ideas on how to make this work pls leave a comment. Essentially I need an async requests with a session, if you know how to do that pls let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够通过简单地将其转换为JSON字符串来在字段中提交完整字典:
根据字典中的数据,您可能需要将序列化类添加到
json.dumps
>处理json.dumps
默认序列化类无法处理的“特殊”数据类型,或者您需要按照某些特殊格式序列化将其序列化为JSON(例如,将DateTime与时区转换为时区服务器期望的文本格式)您还可以将文件添加给add_field的其他呼叫,并指定您在表单字段中的文件名称为
name> name
data的字段.add_field()
调用。在引擎盖下,FormData试图以正确格式的
Multipart/form-data
有效载荷转换所有字段和文件。(我在与AIOHTTP客户端文档和我提交的数据的服务器一整天战斗后发现了大部分内容
I was able to get a submission of a full dictionary in a field by simply converting it to a JSON string:
Depending on the data in the dictionary, you might need to add a serialization class to
json.dumps
to handle "special" data types thatjson.dumps
default serialization class can't handle or for which you need to serialize to JSON following some special format (for example converting a DateTime with time zone to some special text format the server is expecting)You can also add files with additional calls to add_field, specifying the name of the file you have in the form field(s) as the
name
field of yourdata.add_field()
call.Under the hood, FormData tries to transform all your fields and files in a properly formatted
multipart/form-data
payload.(I found out most of this after fighting a whole day with both the aiohttp client docs and the server I was submitting data to that was doing "unsmart" things)