我尝试POST XML并将其存储在datamapper中。 POST格式响应在Postman中显示并且看起来有效,但是当我发出GET请求时,会返回[]。
以下是我使用
的示例XML<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="Test"
tests="4"
failures="0"
errors="0"
skipped="4"
time="0.002">
<properties />
<testcase name=“name” time="0.0" classname="classname">
<skipped />
</testcase>
<testcase name="name" time="0.0" classname="classname">
<skipped />
</testcase>
<testcase name="name" time="0.0" classname="classname">
<skipped />
</testcase>
<testcase name="name" time="0.0" classname="classname">
<skipped />
</testcase>
</testsuite>
来自POST的样本format_response
{"testsuite":null,"testcase":[{"skipped":null,"name":"name","time":"0.0","classname":"classname"},{"skipped":null,"name":"name","time":"0.0","classname":"classname"},{"skipped":null,"name":"name","time":"0.0","classname":"classname"},{"skipped":null,"name":"name","time":"0.0","classname":"classname"}],"name":"Test","tests":"4","failures":"0","time":"0.002","classname":"classname"}
这是我的模特
class SuiteConfiguration
include DataMapper::Resource
property :testsuite, Serial
property :testcase, Serial
property :name, String
property :tests, String
property :failures, String
property :time, String
property :classname, String
end
我的路线
get '/api/tests' do
format_response(SuiteConfiguration.all, request.accept)
end
post '/api/tests' do
body = Crack::XML.parse request.body.read
config = SuiteConfiguration.create(
testsuite: body['testsuite'][0],
testcase: body['testsuite']['testcase'],
classname: body['testsuite']['testcase'][0]['classname'],
name: body['testsuite']['name'],
tests: body['testsuite']['tests'],
failures: body['testsuite']['failures'],
time: body['testsuite']['time'],
)
status 201
format_response(config, request.accept)
end
答案 0 :(得分:0)
我看到它在我的POST响应中没有正确打印后,我将testsuite和testcase属性更改为Object,尽管testsuite仍然显示为null:
class SuiteConfiguration
include DataMapper::Resource
property :id, Serial
property :testsuite, Object
property :testcase, Object
property :name, String
property :tests, String
property :failures, String
property :time, String
property :classname, String
end
并更改了我的帖子以获取testcase中的每个值
post '/api/tests' do
body = Crack::XML.parse request.body.read
config = SuiteConfiguration.create(
testcase: body['testsuite']['testcase'].each do |value|
value
end,
name: body['testsuite']['name'],
tests: body['testsuite']['tests'],
failures: body['testsuite']['failures'],
time: body['testsuite']['time'],
)
status 201
format_response(config, request.accept)
end
现在我收到了一个GET请求,其中包含了datamapper所需的信息。