如何使用formdata在Robot Framework/Python中创建post请求以上载文件和其他键值

2024-09-29 21:26:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力让我的机器人框架代码正常工作。我正在尝试创建一个到我的终点的post请求,名为finance/uploadPayments

此端点包含三个关键值:

  1. files=files,value=选择从目录中选择文件
  2. 用户名=文本,值=尼科尔斯
  3. 分隔符=文本,值=管道

下面是我的机器人框架钥匙:

Process payment
    Create Session  Alias   ${API_URL}
    ${file_data}  Get Binary File  ../tests/data/positive/rca_load_77773_2021-01-10_0953.csv
    ${data}=    Create Dictionary    username=NicoleS  delimiter=pipe
    ${header}=  create dictionary     Accept=text/plain   Content-Type=application/x-www-form-urlencoded
    Set to Dictionary   ${data}
    ${files}  Create Dictionary  files=${file_data}
    Set to Dictionary   ${files}
    Log  ${files}
    Log  ${data}
    ${resp}  RequestsLibrary.Post Request  ALias  finance/uploadPayments  files=${files}  data=${data}  
    headers=${header}
    Log  ${resp}
    Should Be Equal As Strings  ${resp.status_code}  200

请参阅使用邮递员的示例,这很好。 enter image description here

标题:

enter image description here

测试运行结果: enter image description here


Tags: to文本框架logdatadictionarycreate机器人
1条回答
网友
1楼 · 发布于 2024-09-29 21:26:35

我使用Python成功地实现了这一点

import requests
def upload_file(endpoint_url,user_name,file_name,file_path, seperator,file_type):
    url = endpoint_url
    payload = {'username': user_name, 'delimiter': seperator}
    files = {'files': (file_name, open(file_path, 'rb'), file_type, {'Expires': '0'})}
    r = requests.post(url, files=files, data=payload)
    return r.text

相关问题 更多 >

    热门问题