I am very new to testing / developing. I am currently testing a REST API. In Postman I send a POST request by specifying parameters and uploading a file using form-data in the body. My request always succeeds in postman. However, when I try to translate this into Robot Framework (using the Requests Library) file is not getting selected in the request body(displaying content present in a file in the body).
I am posting my code below.
I have been trying to resolve this issue for a while now. Any help in the right direction would be much appreciated!
*** Test Cases ***
TC001
create session mysession ${uri}
${file_name}= Get Binary File ${CURDIR}//test.txt
${asset}= Create Dictionary asset={file_name}
${header}= create dictionary Accept=text/plain Content-Type=multipart/form-data boundary
${response}= post request mysession /api/insertFile data=${file_name} headers=${header}
log to console ${response.content}
#to validate status code with the actual status code
${Status_code}= convert to string ${response.status_code}
should be equal ${Status_code} 200
#to validate the content of the response body
${response_body}= convert to string ${response.content}
should contain ${response_body} true
${filepath} Get File For Streaming Upload ${CURDIR}${/}test.txt
${header}= create dictionary Accept=text/plain Content-Type=multipart/form-data boundary
${response}= post request mysession /api/insertFile asset=${filepath} headers=${header}
log to console ${response.content}
#to validate status code with the actual status code
${Status_code}= convert to string ${response.status_code}
should be equal ${Status_code} 200
#to validate the content of the response body
${response_body}= convert to string ${response.content}
should contain ${response_body} true
Server Log
20200522 02:39:57.418 : INFO : Creating Session using : alias=mysession, url=https://somurl.com, headers={}, cookies={}, auth=None, timeout=None, proxies=None, verify=False, debug=0
20200522 02:39:57.420 : INFO : {filepath} = <_io.BufferedReader name='C:\\Users\\fl792\\eclipse-workspace\\APITesting\\API\\test.txt'>
20200522 02:39:57.424 : INFO : {header} = {âAcceptâ: âtext/plainâ, âContent-Typeâ: âmultipart/form-data boundaryâ}
20200522 02:39:58.791 : INFO : POST Request : url=https://somurl.com/api/insertFile
path_url=/api/insertFile
headers={âUser-Agentâ: âpython-requests/2.23.0â, âAccept-Encodingâ: âgzip, deflateâ, âAcceptâ: âtext/plainâ, âConnectionâ: âkeep-aliveâ, âContent-Typeâ: âmultipart/form-data boundaryâ, âContent-Lengthâ: â96â}
body=asset=<_io.BufferedReader name=âC:\Users\fl792\eclipse-workspace\APITesting\API\test.txtâ>
20200522 02:39:58.793 : INFO : POST Response : url=https://somurl.com/api/insertFile
status=200, reason=OK
body=false
20200522 02:39:58.796 : INFO : {response} = <Response [200]>
20200522 02:39:58.809 : INFO : {Status_code} = 200
20200522 02:39:58.820 : INFO : ${response_body} = false
20200522 02:39:58.826 : FAIL : âfalseâ does not contain âtrueâ
@SuperSonic so status=200 and reason=OK does not indicate successful upload ? are you sure that there should be something in the body ? and you have checked that file was not actually uploaded ? ⊠just confirming some things
Yes there is something in the body not sure what is, I should get a response as true but getting false. Checked with Get requests but not seeing the latest file uploaded.
let me know if any more information required.
Yes, I have tried this before, Get Binary File keyword copies data from the file. I want to upload any specific file it could be .txt file, .docx file, .xls file, .pdf file on the server by using file upload API. Posting details of API from swagger this time hope it will help us to find the issue.!!
Hello, I have completely the same issue as described here. I tried all ways mentioned here but still struggling with finding solution.
SuperSonic: Have you managed to solve it? Thanks