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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-19 08:14:42

问题


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.


回答1:


http://bulkan.github.io/robotframework-requests/#Post has files parameter. And what you could do is use Get File keyword from OperatingSystem library and pass that to your Post keyword.




回答2:


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




回答3:


It perfectly works when using double backslashes and quotes like:

curl -i -H 'Accept: application/json' -H 'Content-Type: application/json' -X POST -d "{\"target\" : \"5142221345\",\"source\" : \"432567890\",\"messages\" : [ { \"format\" : \"AMR\", \"data\" : \"binarydata...\" } ]}" http://10.4.4.11:8089/v1/voice/add



来源:https://stackoverflow.com/questions/21311532/how-to-make-a-post-rest-api-in-robot-framework-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!