简体   繁体   English

如何从发布请求中拆分正文

[英]How to split body from a post request

I am trying to split the body from a post request, and i am wondering what is the best way to do it. 我正在尝试从邮寄请求中拆分尸体,我想知道这样做的最佳方法是什么。 I was thinking on splitting by "&" to extract each parameter, and then by "=" to extract the pair field/value 我正在考虑通过“&”拆分以提取每个参数,然后通过“ =”以拆分对字段/值

data1=value&data2=value2

It will split as follows: 它将拆分如下:

data1 value
data2 value2

However, if the data received contains an "&" or an "=", it will not work: the split method interprets that character in the value as a separator, removing it and creating another field. 但是,如果接收到的数据包含“&”或“ =”,则它将不起作用: split方法将值中的该字符解释为分隔符,将其删除并创建另一个字段。 The best example here is a value received in base64. 最好的示例是在base64中接收的值。 For example, "value" is represented in base64 as dmFsdWU= 例如,“值”在base64中表示为dmFsdWU =

data1=dmFsdWU=&data2=value2

It will split as: 它将拆分为:

data1 dmFsdWU 
data2 value2

Any suggestion on what could i do here? 关于我在这里可以做什么建议? I was thinking on encode the value of the data, so dmFsdWU= is received as dmFsdWU%3D, but i dont know if there is a better solution 我正在考虑对数据的值进行编码,因此dmFsdWU =作为dmFsdWU%3D接收,但是我不知道是否有更好的解决方案

Thanks 谢谢

Use the second parameter of split to limit the splitting: 使用split的第二个参数限制拆分:

in_str = "data1=dmFsdWU=&data2=value2"
param_list = [param_expr.split('=', 1) for param_expr in in_str.split('&')]

Result: 结果:

[['data1', 'dmFsdWU='], ['data2', 'value2']]

Deficiency 不足

You haven't described your full interpretation grammar: what to do if the first value contains a & . 您尚未描述完整的解释语法:如果第一个值包含&该怎么办。 For instance, change your example to 例如,将您的示例更改为

data1=dmFsd&WU&data2=value2 DATA1 = dmFsd&WU&DATA2 =值2

Where you intend 你打算去哪里

data1 dmFsd&WU
data2 value2

What parsing rules obtain here? 在这里获得什么解析规则? Are there restrictions on the field name that will disambiguate this? 字段名称是否有限制,以消除歧义? For instance, if the field name must be alphanumeric, then your parsing job is possible, but becomes a little trickier: 例如,如果字段名称必须为字母数字,那么您的解析工作是可能的,但会变得有些棘手:

  • Find the first = ; 找到第一个= ; this determines the first field name 这确定了第一个字段名称
  • Find the next & ... 查找下一个& ...
  • Find the next = ; 找到下一个= ; this is the end of the second field name 这是第二个字段名称的结尾
  • Locate the alphanumeric sequence ending at that latter = ; 找到以后者=结尾的字母数字序列; use this as the second field name. 使用它作为第二个字段名称。

    You have now determined the two field names; 现在,您已经确定了两个字段名称; the remaining two strings are the values. 其余两个字符串是值。


HOWEVER 然而

Note that there are pathological strings the cannot be parsed uniquely into a pair of field/value pairs. 请注意,有些病理字符串无法唯一地解析为一对字段/值对。 Most simply, let's take your given example: 最简单地,让我们举一个给定的例子:

data1=value&data2=value2

Why is this not a single field/value pair? 为什么这不是单个字段/值对?

data1 value&data2=value2

You will run into this any time a value is allowed to contain both & and = . 只要允许值同时包含&=就会遇到这种情况。

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

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