These days to tune a multipart/form-data type of interface, encountered a small obstacle. Former colleagues have used the Urllib library to write a similar method to achieve, relatively long, want to change the time found not very good. Find on the Web find using the requests library to do this more powerful. The following specific introduction python-requests and Robotframework-requestslibrary implement Multipart/form-data interface upload file.
1, from the Fiddler view interface length such as:
Header:
WebForms:
2, python-requests realization
#!/usr/bin/env python
#-*-Coding:utf-8-*-
Import Requests #导入依赖模块
#构造数据, set file as a tuple dictionary form, see the network also has the form of a list, no walk through.
#示例接口是普通的字段和文件混合传的, so according to the line above the corresponding down, this is the way
#为什么用这个形式, view official documents: #http://docs.python-requests.or. Files
Multiple_files = {' id ':(None, ' 181 '),
' title ':(None,u "pull the new activity"),
' Forceexternalbrowser ':(none,u "no"),
' Type ':(none,u "article"),
' Location ':(None, ' http://${domain}/activity/appointment/?id=44&_=360 '),
' Picurl ': (' 1.png ', open (' e:\\pics\\1.png ', ' RB '), ' Image/png ')}
#接口url
Server_url= ' Http://${domain}/cn-jsfund-client-manage/manage/cms/banner/add '
#示例接口需登录, so I sent a cookie and put it in the header.
Headers1 = {' Cookie ': ' jsessionid=fd1ccb5abfd79a055031b390397c411a '}
#请求接口
Response=requests.post (SERVER_URL,FILES=MULTIPLE_FILES,HEADERS=HEADERS1)
Print Response.text
3, Robotframework-requestslibrary realization
Here is the RF case, without a complete suite, remember to import the library Requestslibrary first:
{{{{} update a banner
${id_tup} Evaluate (None, ' 181 ') #创建tuple, because I don't know how to create a tuple on RF, so I used eval
${title_tup} Evaluate (none,u "Pull new event")
${for_tup} Evaluate (None,u "no")
${type_tup} Evaluate (none,u "article")
${pic_tup} Evaluate (' 1.png ', open (' e:/pics/1.png ', ' RB '), ' image/png ')
#然后创建file字典, like the Python one.
&{multiple_files} Create Dictionary Id=${id_tup} title=${title_tup} Forceexternalbrowser=${for_tup} type=${type_ Tup} Picurl=${pic_tup}
#同样的: Set Header
${header} Create Dictionary cookie=jsessionid=0c800a4042d35bc62487e71431c2242d
#这个注释掉了, using the Requests library Post is also possible to send this request, verify
Comment ${res} requests. Post ${domain}/cn-jsfund-client-manage/manage/cms/banner/add files=${multiple_files} Headers=${header}
#下面两行, is the real requestslibrary keyword, as to how long this, please F5
#请注意, the header must be written down here.
Create Session API ${domain} ${header}
${res} Post Request api/cn-jsfund-client-manage/manage/cms/banner/add \ \ ${header} ${multiple_files}
Log ${res.content}}}}
Above, for the Robotframework-requests (0.4.4) version of the implementation method. The sample interface is a mixed pass for normal fields and files. Requestslibrary author in October fix this bug (https://github.com/bulkan/robotframework-requests/issues/131), You can handle this situation in a more concise way:
Don't write your own content-type! in the header.
Uploading files using Python or Robotframework Multipart/form-data interface