Skip to content

Commit 6afd1ce

Browse files
authored
Merge pull request #338 from MarketSquare/docs/post-multipart
Improved documentation for post multipart
2 parents 6c97257 + 75ed52a commit 6afd1ce

File tree

5 files changed

+31
-54
lines changed

5 files changed

+31
-54
lines changed

atests/test_post_multipart.robot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ Test Post On Session Multipart
1717
${resp}= POST On Session ${GLOBAL_SESSION} /anything files=${files}
1818

1919
Should Contain ${resp.json()}[headers][Content-Type] multipart/form-data; boundary=
20+
Should Contain ${resp.json()}[headers][Content-Length] 480
2021
Should Contain ${resp.json()}[files] randombytes1
22+
Should Contain ${resp.json()}[files] randombytes2

atests/testcase.robot

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -218,82 +218,33 @@ Post Request With File
218218

219219
Post Request With Data and File
220220
[Tags] post
221-
Create Session httpbin http://httpbin.org
222221
&{data}= Create Dictionary name=mallikarjunarao surname=kosuri
223222
Create File foobar.txt content=foobar
224223
${file_data}= Get File foobar.txt
225224
&{files}= Create Dictionary file=${file_data}
226-
${resp}= Post Request httpbin /post files=${files} data=${data}
225+
${resp}= Post Request ${test_session} /anything files=${files} data=${data}
227226
Should Be Equal As Strings ${resp.status_code} 200
228227

229228
Put Requests
230229
[Tags] put
231-
Create Session httpbin http://httpbin.org
232230
&{data}= Create Dictionary name=bulkan surname=evcimen
233231
&{headers}= Create Dictionary Content-Type=application/x-www-form-urlencoded
234-
${resp}= Put Request httpbin /put data=${data} headers=${headers}
232+
${resp}= Put Request ${test_session} /anything data=${data} headers=${headers}
235233
Dictionary Should Contain Value ${resp.json()['form']} bulkan
236234
Dictionary Should Contain Value ${resp.json()['form']} evcimen
237235

238-
Head Request
239-
[Tags] head
240-
Create Session httpbin http://httpbin.org
241-
${resp}= Head Request httpbin /headers
242-
Should Be Equal As Strings ${resp.status_code} 200
243-
244-
Options Request
245-
[Tags] options
246-
Create Session httpbin http://httpbin.org
247-
${resp}= Options Request httpbin /headers
248-
Should Be Equal As Strings ${resp.status_code} 200
249-
Dictionary Should Contain Key ${resp.headers} allow
250-
251-
Delete Request With URL Params
252-
[Tags] delete
253-
Create Session httpbin http://httpbin.org
254-
&{params}= Create Dictionary key=value key2=value2
255-
${resp}= Delete Request httpbin /delete ${params}
256-
Should Be Equal As Strings ${resp.status_code} 200
257-
258-
Delete Request With No Data
259-
[Tags] delete
260-
Create Session httpbin http://httpbin.org
261-
${resp}= Delete Request httpbin /delete
262-
Should Be Equal As Strings ${resp.status_code} 200
263-
264-
Delete Request With Data
265-
[Tags] delete
266-
Create Session httpbin http://httpbin.org debug=3
267-
&{data}= Create Dictionary name=bulkan surname=evcimen
268-
${resp}= Delete Request httpbin /delete data=${data}
269-
Should Be Equal As Strings ${resp.status_code} 200
270-
Log ${resp.content}
271-
Comment Dictionary Should Contain Value ${resp.json()['data']} bulkan
272-
Comment Dictionary Should Contain Value ${resp.json()['data']} evcimen
273-
274-
Delete Requests with Json Data
275-
[Tags] delete
276-
Create Session httpbin http://httpbin.org
277-
&{data}= Create Dictionary latitude=30.496346 longitude=-87.640356
278-
${resp}= Delete Request httpbin /delete json=${data}
279-
Should Be Equal As Strings ${resp.status_code} 200
280-
${jsondata}= Set Variable ${resp.json()}
281-
Should Be Equal ${jsondata['json']} ${data}
282-
283236
Patch Requests
284237
[Tags] patch
285-
Create Session httpbin http://httpbin.org
286238
&{data}= Create Dictionary name=bulkan surname=evcimen
287239
&{headers}= Create Dictionary Content-Type=application/x-www-form-urlencoded
288-
${resp}= Patch Request httpbin /patch data=${data} headers=${headers}
240+
${resp}= Patch Request ${test_session} /anything data=${data} headers=${headers}
289241
Dictionary Should Contain Value ${resp.json()['form']} bulkan
290242
Dictionary Should Contain Value ${resp.json()['form']} evcimen
291243

292244
Patch Requests with Json Data
293245
[Tags] patch
294-
Create Session httpbin http://httpbin.org
295246
&{data}= Create Dictionary latitude=30.496346 longitude=-87.640356
296-
${resp}= Patch Request httpbin /patch json=${data}
247+
${resp}= Patch Request ${test_session} /anything json=${data}
297248
Should Be Equal As Strings ${resp.status_code} 200
298249
${jsondata}= Set Variable ${resp.json()}
299250
Should Be Equal ${jsondata['json']} ${data}

doc/RequestsLibrary.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/RequestsLibrary/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,30 @@ class RequestsLibrary(RequestsOnSessionKeywords, DeprecatedKeywords):
9494
| status_code | Integer Code of responded HTTP Status, e.g. 404 or 200. |
9595
| text | Content of the response, in unicode. If ``response.encoding`` is ``None``, encoding will be guessed using chardet. The encoding of the response content is determined based solely on HTTP headers, following RFC 2616 to the letter. If you can take advantage of non-HTTP knowledge to make a better guess at the encoding, you should set ``response.encoding`` appropriately before accessing this property. |
9696
| url | Final URL location of Response. |
97+
98+
= POST a Multipart-Encoded File =
99+
100+
RequestsLibrary makes it simple to upload Multipart-encoded files, but in order to make sure that the
101+
Python Library provides automatically the right ``Content-Length`` and ``multipart/form-data; boundary=...``
102+
headers you SHOULD NOT provide those headers manually, use the keyword
103+
`Get File For Streaming Upload` instead that opens the files in binary mode.
104+
105+
Below an example of multiple file sent over a single POST:
106+
107+
| Test Post Multiple Files
108+
| ${file_1}= Get File For Streaming Upload files/randombytes.bin
109+
| ${file_2}= Get File For Streaming Upload files/randombytes.bin
110+
| ${files}= Create Dictionary randombytes1 ${file_1} randombytes2 ${file_2}
111+
|
112+
| ${resp}= POST https://someurl files=${files}
113+
114+
You can find a working test example in `atests/test_post_multipart.robot`.
115+
116+
For a complete reference verify the official Requests documentation:
117+
118+
- https://2.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file
119+
- https://2.python-requests.org/en/master/user/advanced/#post-multiple-multipart-encoded-files
120+
97121
"""
98122
__version__ = VERSION
99123
ROBOT_LIBRARY_SCOPE = 'GLOBAL'

tests-report/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)