简体   繁体   中英

How to create a post request in Robot Framework/ Python using form-data to upload files and other key values

I am struggling to get my Robot Framework code working. I am trying to create a post request to my End point Called finance/uploadPayments .

This endpoint takes in three key values:

  1. files = files, value = select to choose the file from the directory .
  2. username = text, value = NicoleS
  3. delimiter = text, value = pipe

below is my robot frame keywork:

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

Please see example using Postman, this works fine. 在此处输入图像描述

headers:

在此处输入图像描述

Test Run Results: 在此处输入图像描述

I managed to get this working using 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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