Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a POST (ReST) API in Robot framework with

I need to replicate the below API call in Robot Framework:

curl -X POST "http://xyz/api/createApp" -H "Content-Type:application/json" -d @/tmp/testfile.json

testfile.json has a json payload. I cannot send the content of Json file as body.

I have imported the HTTP libraries. But do not see any key-word to make an API call with file.

like image 630
Deepti K Avatar asked Jan 23 '14 14:01

Deepti K


1 Answers

Bulkan's robotframework-requests is nice. But if you can get by with less, you can do your own local lib/posthttp.py in a few lines like this:

import requests
import json

def do_requests_post( url=None, data=None, headers={"Content-Type":"application/json"}):
    return requests.post( url, data=data, headers=json.loads(headers) )

def do_requests_request( method="GET" url=None, data=None, headers={}):
    return requests.request( url, method=method, data=data, headers=json.loads(headers))

Note that the return object is the rich-and-powerful "Response" which has member functions like .json() (which returns a dict if the .text is sensed to be JSON) and .status_code (int).

like image 156
MarkHu Avatar answered Oct 11 '22 03:10

MarkHu