简体   繁体   English

如何在Robot框架中创建POST(ReST)API

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

I need to replicate the below API call in Robot Framework: 我需要在Robot Framework中复制以下API调用:

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

testfile.json has a json payload. testfile.json有一个json有效负载。 I cannot send the content of Json file as body. 我无法将Json文件的内容作为正文发送。

I have imported the HTTP libraries. 我导入了HTTP库。 But do not see any key-word to make an API call with file. 但是没有看到用文件进行API调用的任何关键词。

Bulkan's robotframework-requests is nice. Bulkan的机器人框架要求很好。 But if you can get by with less, you can do your own local lib/posthttp.py in a few lines like this: 但是如果你能用更少的东西,你可以在几行中做你自己的本地lib / posthttp.py:

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). 请注意,返回对象是功能丰富的“响应”,它具有成员函数,如.json() (如果感知.text.status_code为JSON则返回dict)和.status_code (int)。

http://bulkan.github.io/robotframework-requests/#Post has files parameter. http://bulkan.github.io/robotframework-requests/#Post有files参数。 And what you could do is use Get File keyword from OperatingSystem library and pass that to your Post keyword. 您可以做的是使用OperatingSystem库中的Get File关键字并将其传递给Post关键字。

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 curl -i -H'Eccept: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 /语音/加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM