Reputation: 33
Background: New to Robot Framework but attempting at using the RequestsLibrary together with OperatingSystem and XML to automate some REST testing on RIDE.
Requirement: Sending file with XML body and validate after receiving the XML response from the server.
Problems: Current setup leading to 500 internal server error.
Is it possible to send the content of the XML file to the server instead of the XML file itself?
Is it possible to return the content of the response from the server instead of the response code?
Have spent a while searching already for pointers but not found anything overly helpful with most requests and response dealing with JSON instead of XML.
Ideal flow:
1)extract XML file content from file in directory
2)send as POST request to server URL
3)wait for response
4)return this XML response and validate it is correct
CODE:
*** Settings ***
Library Collections
Library RequestsLibrary
Library XML
Library Selenium2Library
Library OperatingSystem
*** Test Cases ***
Example 1
Create Session Gateway https://GatewayURL.asmx
${file_data}= Get Binary File ${CURDIR}${/}data.xml
${files}= Create Dictionary ${file_data}
${response}= Post Request Gateway /post files=${files}
I've added to code using the helpful suggestions.
Post request data in dictionary SAMPLE
Create Session Gateway URL HERE debug=3
${file_data}= Get Binary File ${CURDIR}${/}data.xml
&{data}= Create Dictionary name=${file_data.strip()}
&{headers}= Create Dictionary Content-Type=text/xml
${resp}= Post Request Gateway /post data=${data} headers=${headers}
Should Be Equal As Strings ${resp.status_code} 200
N.B URL HERE used as unfortunately not able to share this URL.
Failure is as follows. Not sure why data is text/xml when this has been used for the header. This is causing a server 500 error.
Post Request using : alias=Gateway, uri=/post, data=<text/xml>, headers={u'Content-Type': u'text/xml'}, files=None, allow_redirects=True
500 != 200
I want to send the content of the XML file not the XML file itself as the server 'wont know how to handle' the file but will be able to 'handle' the actual content of the XML file.
Have you encountered this problem before where RF is not accepting the data argument for the Post Request Keyword? It's always using the Header argument for Data for some reason...
Upvotes: 1
Views: 5656
Reputation: 20077
The return of Put Request
(and the other request types in the lib) is a Response object, from the python's requests library. As such, you have full access to its attributes and methods, described in the link above.
For example, if you want the payload of the response:
Log To Console ${response.text}
${payload}= Set Variable ${response.text}
# process/verify the server's responose, it's now in the ${payload} variable
(if the server's response is not Unicode, you might want to check the doc for text
and explicitly set the correct one)
Another example - if you need a particular header:
Log To Console ${response.headers['content-encoding']}
# or, this will print them all:
Log To Console ${response.headers}
Final example, the response's status code:
Should Be Equal As Strings ${response.status_code} 200
RE: the updated question, how to actually send a file with a PUT request. In the code you've shown, there's this error - you're reading the file here:
...
${file_data}= Get Binary File ${CURDIR}${/}data.xml
Then, storing its contents as a value in a dictionary:
&{data}= Create Dictionary name=${file_data.strip()}
And sending the dictionary as payload:
${resp}= Post Request Gateway /post data=${data} headers=${headers}
I am highly suspicious this is in the receiving service's specs - most likely it expects the contents of the file in the payload; also, it probably expects the filename as name
header (but I'm guessing here, adapt to the actual reqs). So the flow is probably:
Create Session Gateway URL HERE debug=3
${file_data}= Get Binary File ${CURDIR}${/}data.xml
&{headers}= Create Dictionary Content-Type=text/xml name=data.xml
${resp}= Post Request Gateway /post data=${file_data} headers=${headers}
Should Be Equal As Strings ${resp.status_code} 200
Anyways, it's a guessing game w/o knowing the service; if that ^ doesn't help, check its logs and talk with the developers - what is the correct format of the request.
Upvotes: 0