Home Get file returned from REST POST request with robot framework requests library
Reply: 1

Get file returned from REST POST request with robot framework requests library

Zeinab Abbasimazar
1#
Zeinab Abbasimazar Published in 2018-02-14 12:25:52Z

I am testing a REST server; one of its services provides a file in response. I use robotframework-requests library for calling the service. This is my test-case:

*** Settings ***
Library             RequestsLibrary


*** Test Cases ***
Test
    RequestsLibrary.CREATE SESSION      session     http://192.168.100.139:9090
    ${data} =       evaluate    json.load(open('/root/data.json', 'r'))   json
    log to console  Data: ${data}
    ${resp} =       RequestsLibrary.POST REQUEST    session  uri=/omc/simulator/api/generate   data=${data}
    log to console  Resp: ${resp}

This is what I get in return:

Data: {u'files': [{u'tags': [{u'name': u'l1', u'value': u'l2', u'level': 2}], u'counters': {u'count': 10, u'valueMin': 0, u'prefix': u'a1', u'specifics': [{u'valueMin': 0, u'valueMax': 10, u'name': u'a2', u'tags': {u'additionalProp3': u'a5', u'additionalProp2': u'a4', u'additionalProp1': u'a3'}}], u'valueMax': 10}}], u'to': u'2018-02-14T11:25:10.712Z', u'from': u'2018-02-14T11:05:10.712Z', u'intervalMillis': 300000}
Resp: <Response [415]>

Where 415 HTTP code means Unsupported Media Type.

How can I get the provided file in return and how should I config the path of download? Is it feasible with coding in robotframework or python?

EDIT 1:

I have tried setting Content-Type for request header as @Todor suggested:

${headers} =    Create Dictionary    Content-Type=${contentType}
${resp} =       RequestsLibrary.POST REQUEST    session  uri=/omc/simulator/api/generate   data=${data}   headers=${headers}

I still get status code 415 for both application/octet-stream and application/zip (the file has .zip extension) as ${contentType}.

EDIT 2:

I changed the server in order to save the file into a directory, so I can use it; but since it doesn't accept my header, I still can't completely call the service. I get 415 status code, which is sent from server implying that I'm not setting right Content-type in my header.

BTW, I changed my request body as @Todor recommended:

${data} =       evaluate    json.dumps(json.load(open('/root/data.json', 'r')))  json
${headers} =    Create Dictionary    Content-type=${contentType}
${headers} =    evaluate    json.dumps(${headers})
${resp} =       RequestsLibrary.POST REQUEST    session  uri=/omc/simulator/api/generate   data=${data}   headers=${headers}

But I tried it for both application/octet-streamandapplication/zip(the file has.zipextension) as${contentType}`; still getting same error.

** EDIT 3:**

I was using wrong Content-type all the time; I should have used application/json. Considering @Todor's notes about header and data, I finally get it work.

${data} =       evaluate    json.dumps(json.load(open('/root/data.json', 'r')))  json
${headers} =    Create Dictionary    Content-type=application/json
${resp} =       RequestsLibrary.POST REQUEST    session  uri=/omc/simulator/api/generate   data=${data}   headers=${headers}
Todor
2#
Todor Reply to 2018-02-15 10:45:49Z

With this error response you've probably missed a required header, like content-type; check your app's logs to be sure.
Here's how to add it:

# the previous code
${headers}=   Create Dictionary   Content-type=application/json
${resp} =       RequestsLibrary.POST REQUEST    session  uri=/omc/simulator/api/generate   data=${data}   headers=${headers}

There is another issue with the code; this line here:

${data} =       evaluate    json.load(open('/root/data.json', 'r'))   json

,will effectively load the provided json file, and transform it into a python object (most likely, a dictionary).
Yet, you are not supposed to send it like this in the request - it has to be serialized back into a json format (the biggest difference b/n json and string representation of python dictionaries - json does not use single quotes ' as delimiters).

So once you've done that, you can do:

${data} =       evaluate    json.dumps($data)   json

, which will transform it back. Or, just load the file contents and not do this forth-and-back transformations.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302106 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO