简体   繁体   English

AioHttp:如何为 aiohttp 帖子创建数据表单

[英]AioHttp: How can I create a data form for an aiohttp post

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.我正在尝试转换它以便它可以与 aiohttp 一起使用,这就是我所拥有的。 I think I'm getting an error because of line: "action": {"name":"EMAIL_REG_FORM_SUBMIT"},我想我因为行而收到错误: "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.本质上,我需要一个带有会话的异步请求,如果您知道该怎么做,请告诉我。

I was able to get a submission of a full dictionary in a field by simply converting it to a JSON string:通过简单地将其转换为 JSON 字符串,我能够在字段中提交完整字典:

data.add_field("action", json.dumps({"name":"EMAIL_REG_FORM_SUBMIT"}))

Depending on the data in the dictionary, you might need to add a serialization class to json.dumps to handle "special" data types that json.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)根据字典中的数据,您可能需要将序列化类添加到json.dumps以处理json.dumps默认序列化类无法处理的“特殊”数据类型,或者您需要在一些特殊之后序列化为 JSON格式(例如将带时区的 DateTime 转换为服务器期望的某种特殊文本格式)

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 your data.add_field() call.您还可以通过额外调用 add_field 添加文件,将表单字段中的文件名称指定为data.add_field()调用的name字段。

Under the hood, FormData tries to transform all your fields and files in a properly formatted multipart/form-data payload.在幕后,FormData 尝试将所有字段和文件转换为格式正确的multipart/form-data有效负载。

(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) (在与 aiohttp 客户端文档和我向其提交数据的服务器进行了一整天的斗争之后,我发现了其中的大部分内容正在做“不智能”的事情)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM