本质上,我尝试做的是发布到REST API,但无论我做什么,我最终都会得到HTTP 400。以下是我的非常快速和非常肮脏的代码:
package require rest
package require json
::http::register https 443 ::tls::socket
set credentials {username admin password LabPass1}
set url1 [format "%s/%s" "https://127.0.0.1:8834" session]
set unformattedToken [dict get [::json::json2dict [::rest::post $url1 $credentials]] token]
set cookie [format "token=%s" $unformattedToken]
set header [list X-Cookie $cookie Content-type application/json]
set config [list method post format json headers $header]
set url [format "%s/%s" "https://127.0.0.1:8834" scans]
set uuid 7485-2345-566
set name "Testing TCL Network Scan"
set desc "Basic Network Scan using API"
set pid 872
set target 127.0.0.1
set data {{"uuid":"$uuid","settings": {"name":"$name","description":"$desc", "policy_id":"$pid","text_targets":"$target", "launch":"ONETIME","enabled":false,"launch_now":true}}}
set jsonData [json::json2dict $data]
set response [::rest::simple $url $jsonData $config]
我试过使用上面的代码,也试过删除json::json2dict调用,只发送数据。我相信,我可能是错的,我的问题是数据是基于行的文本数据:
POST /scans HTTP/1.1
Host: 127.0.0.1:8834
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 10.0) http/2.8.9 Tcl/8.6.4
Connection: close
X-Cookie: token=301b8dcdf855a29b5b902cf8d93c49750935c925a965445e
Content-type: application/json
Accept: */*
Accept-Encoding: gzip,deflate,compress
Content-Length: 270
uuid=7485-2345-566&settings=name%20%7BTesting%20TCL%20Network%20Scan%7D%20description%20%7BBasic%20Network%20Scan%20using%20API%7D%20policy_id%20872%20text_targets%20127.0.0.1%20launch%20ONETIME%20enabled%20false%20launch_now%20true
我已经阅读了JSON文档和REST文档,但我很难找到使用JSON格式发布的示例。下面是curl命令的外观:
curl https://127.0.0.1:8834/scans -k -X POST -H 'Content-Type: application/json' -H 'X-Cookie: token= <token>' -d '{"uuid":"7485-2345-566","settings":{"name":"Testing TCL Network Scan","description":"Basic Network Scan using API", "policy_id":"872","text_targets":"127.0.0.1", "launch":"ONETIME","enabled":false,"launch_now":true}'
2条答案
按热度按时间vfh0ocws1#
您遇到的一个问题是查询中的值没有被计算。例如,
"uuid":"$uuid"
变为"uuid":"$uuid"
。这是因为data
被设置为的值周围有大括号。最好的解决方案似乎不是创建一个json对象,然后将其转换为
dict
,而是直接创建dict
,如下所示:或者像这样,对于较短的行:
或者通过一些其它方法。
文件:dict、list、set
xxhby3vn2#
我成功地:
设置响应[::rest::简单$url“”$配置$jsonData]
文件:::rest::简单的url查询?配置??主体?
“”是查询参数,我不需要它,所以它是空的
JsonData在我的例子中是一个json字符串,我将尝试使用字典来看看它是否工作。