Robotframework.request - 如何使用内容“multipart/form-data"发出 POST 请求; [英] Robotframework.request - How to make a POST request with content "multipart/form-data"

查看:53
本文介绍了Robotframework.request - 如何使用内容“multipart/form-data"发出 POST 请求;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 RequestsLibrary 但似乎没有任何效果.发出此请求的关键字如下所示:

I want to make a POST request in Robot Framework with "Content-Type: multipart/form-data" using the RequestsLibrary but nothing seems to work. The Keyword that makes this request looks as follows:

*** Variables ***
&{API_CREDS}  username=myusername  password=mypwd

*** Keywords ***
Get token
    # Assumes that session has been created
    [Arguments]  ${Session_id}
    &{headers}=  create dictionary  Content-Type=multipart/form-data
    ${response}=  Post Request  ${Session_id}  ${AUTH_TOKEN_URL_PATH}  data=${API_CREDS}   headers=${headers}
    should be equal as integers  ${response.status_code}  200
    [Return]  ${response.json()['token']}

但实际发送的 POST 请求不包含Content-Type"标头,主体只是一个原始的 data={'username' = 'myusername', 'password' = 'mypwd'}

But the POST request that is actually sent does not contain a "Content-Type" header and the body is just a raw data={'username' = 'myusername', ' password' = 'mypwd'}

我尝试了很多我发现的东西,但没有任何效果.Robot Framework 的 RequestsLibrary 是否真的支持发送带有Content-Type: multipart/form-data"的 POST 请求?如果支持,这是怎么做的?

I have tried many things that I have found searching around but nothing works. Does the RequestsLibrary of Robot Framework actually supports sending a POST request with "Content-Type: multipart/form-data"?, if so how is this done?

PS:我在 Windows 10 上使用 Robot Framework 和 Python 3.7.1.POST 请求实际上已发送,但它不包含 Content-Type 标头,也不包含上面提到的表单数据负载.

PS: I am using Robot Framework on Windows 10 with Python 3.7.1. The POST request is actually sent, but it does not contain a Content-Type header, nor a form-data payload, as mentioned above.

推荐答案

所使用的底层 Python 库 - requests 具有处理多部分表单数据"内容的一些特殊性.它主要使用它作为请求的一部分发送文件(上传功能);粗略地说,当它解析你的参数时,它剥离了标题,因为没有要发送的文件.此外,如果它没有这样做,它的设计目的不是为了扣除多部分有效载荷中的不同部分 - 例如它不会自动将每个键值对放在单独的部分中.

The underlying python library that's used - requests , has some peculiarities working with multipart "form-data" content. It uses it primary for sending files as part of the request (an upload functionality); roughly speaking when it parsed your arguments, it stripped the header because there were no files to be sent. Also, if it didn't do that, it's not designed to deduct what are the different parts in your multipart payload - e.g. it doesn't automatically put every key-value pair in a separate part.

为了克服这一点,通常使用 files 参数,将不同部分的内容作为参数传递.这样做时,requests 库将自动设置表单数据标头,并将内容分成几部分.
下面是如何在 RF 中做到这一点,解释如下:

To overcome this, one usually uses the files parameter, passing as argument the content of the different parts. In doing so, the requests library will automatically set the form-data header, and break-up the content in parts.
Here's how to do that in RF, explanation follows:

${data}=    Evaluate    {'username': (None, 'myusername'), 'password': (None, 'mypwd')}
${response}=  Post Request  ${Session_id}  ${AUTH_TOKEN_URL_PATH}  files=${data}

使用 Post Request 关键字中的 files 参数,您的有效负载将按原样传递给 requests post 方法.您不需要显式设置标题,库会为您完成.

Using the files parameter in the Post Request keyword your payload will be passed to the requests post method as is. You don't need to set the headers explicitly, the library will do it for you.

作为参数传递的是字典,值是部件的内容.如您所见,实际值是 python 元组,因为您想覆盖部件中的文件名.用一个例子可以更好地解释这一点;如果数据是这样的,价值是一个简单的刺:

What is passed as argument is a dictionary, were the values are the parts' content. As you can see the actual values are python tuples, because you want to override the filename in the part. This is better explained with an example; if the data is like this, the value being a simple sting:

${data}=    Evaluate    {'username': 'myusername', 'password': 'mypwd'}

,那么payload会变成:

, then the payload will turn up as:

--7579227dh785568ha91866339229add786
Content-Disposition: form-data; name="username"; filename="username"

myusername
--7579227dh785568ha91866339229add786
Content-Disposition: form-data; name="password"; filename="password"

mypwd
--7579227dh785568ha91866339229add786--

注意每个部分如何有一个文件名"属性,等于参数名.

Notice how each part has a "filename" property, equal to the parameter name.

当值为元组时,其第一个成员设置部件的文件名"属性;当它是 None 时,根本没有文件名",产生这样的结果:

When the value is a tuple, its first member sets the "filename" property of the part; and when it is a None, there is no "filename" at all, producing this result:

--7579227dh785568ha91866339229add786
Content-Disposition: form-data; name="username"

myusername
--7579227dh785568ha91866339229add786
Content-Disposition: form-data; name="password"

mypwd
--7579227dh785568ha91866339229add786--

,这可能是您的目标.

这篇关于Robotframework.request - 如何使用内容“multipart/form-data"发出 POST 请求;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆