1

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...

4
  • 1
    Can you edit your question and update the example with publicly available URL's so that we can replicate your results? Can you also add the actual error message received from the system? Commented Jan 25, 2018 at 16:20
  • It's not fully clear what is the actual problem you're facing? I'm sorry but I don't understand the two bullet points in the question, and their context. If the problem is "how to get the server's response and work with it", then read on in the answer :) Commented Jan 26, 2018 at 5:04
  • @RangHu so it looks like the answer is what you were looking for; I'd suggest to please edit the question - remove the bullets (they are confusing), replace them with something in the line of "how can I get the payload of the server's response, for further verification?". Commented Jan 26, 2018 at 10:05
  • 1
    Thanks for the suggestions, will update the question info. Commented Jan 26, 2018 at 10:12

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.