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

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

user3454
1#
user3454 Published in May 21, 2018, 4:49 pm

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}
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO