【问题标题】:Robot Framework: send binary data in POST request body with机器人框架:在 POST 请求正文中发送二进制数据
【发布时间】:2019-07-21 06:45:41
【问题描述】:

我在使用 Robot Framework 和 robotframework-requests 运行测试时遇到问题。我需要在正文中发送 POST 请求和二进制数据。我已经看过this question,但并没有真正回答。这是我的测试用例的样子:

Upload ${filename} file
    Create Session  mysession     http://${ADDRESS}
    ${data} =   Get Binary File     ${filename}
    &{headers} =    Create Dictionary   Content-Type=application/octet-stream   Accept=application/octet-stream
    ${resp} =   Post Request    mysession     ${CGIPath}  data=${data}    headers=&{headers}
    [Return]    ${resp.status_code}     ${resp.text}

问题是我的二进制数据大约是 250MB。当使用Get Binary File 读取数据时,我看到内存消耗高达 2.x GB。几秒钟后,当Post Request 被触发时,我的测试被OOM 杀死。我已经看过files参数,但它似乎使用多部分编码上传,这不是我需要的。

我的另一个想法是将打开的文件处理程序直接传递给底层请求库,但我想这需要修改机器人框架请求。另一个想法是只为这个测试回退到 curl。

我是否在测试中遗漏了什么?解决这个问题的更好方法是什么?

【问题讨论】:

    标签: python python-requests http-post robotframework


    【解决方案1】:

    我继承了robotframework-request修改的想法,添加了这个方法

    def post_request_binary(                                                                                          
            self,                                                                                                     
            alias,
            uri,
            path=None,
            params=None,
            headers=None,
            allow_redirects=None,                                                                                     
            timeout=None):      
    
        session = self._cache.switch(alias)                                                                           
        redir = True if allow_redirects is None else allow_redirects                                                  
        self._capture_output()  
    
        method_name = "post"    
        method = getattr(session, method_name)                                                                        
    
        with open(path, 'rb') as f:                                                                                   
            resp = method(self._get_url(session, uri),                                                                
                          data=f,                                                                                     
                          params=self._utf8_urlencode(params),                                                        
                          headers=headers,                                                                            
                          allow_redirects=allow_redirects,                                                            
                          timeout=self._get_timeout(timeout),                                                         
                          cookies=self.cookies,                                                                       
                          verify=self.verify)                                                                         
    
        self._print_debug()                                                                                           
    
        # Store the last session object                                                                               
        session.last_resp = resp
    
        self.builtin.log(method_name + ' response: ' + resp.text, 'DEBUG')                                            
    
        return resp
    

    我想我可以稍微改进一下并创建一个拉取请求。

    【讨论】:

      猜你喜欢
      • 2022-08-12
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 2019-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多