Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在TCL中使用REST以JSON格式发布?_Json_Rest_Curl_Tcl - Fatal编程技术网

在TCL中使用REST以JSON格式发布?

在TCL中使用REST以JSON格式发布?,json,rest,curl,tcl,Json,Rest,Curl,Tcl,本质上,我试图做的是将其发布到RESTAPI,但无论我做什么,最终都会得到HTTP400。以下是我的极快且极脏的代码: 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" sessi

本质上,我试图做的是将其发布到RESTAPI,但无论我做什么,最终都会得到HTTP400。以下是我的极快且极脏的代码:

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}' 
curlhttps://127.0.0.1:8834/scans -k-X POST-H'内容类型:application/json'-H'X-Cookie:token='-d'{“uuid”:“7485-2345-566”,“设置”:{“名称”:“测试TCL网络扫描”,“描述”:“使用API的基本网络扫描”,“策略id”:“872”,“文本目标”:“127.0.0.1”,“启动”:“一次性”,“启用”:false,“立即启动”:true}

您遇到的一个问题是查询中的值没有计算。
的“uuid”:“$uuid”
变成了
“uuid”:“$uuid”
。这是因为
数据设置为的值周围有大括号

最好的解决方案似乎不是创建json对象,然后将其转换为
dict
,而是直接创建
dict
,如下所示:

set data [list uuid $uuid settings [list name $name description $desc policy_id $pid text_targets $target launch ONETIME enabled false launch_now true]]
或者像这样,对于较短的行:

dict set data uuid $uuid
dict set data settings name $name
dict set data settings description $desc
dict set data settings policy_id $pid
dict set data settings text_targets $target
dict set data settings launch ONETIME
dict set data settings enabled false
dict set data settings launch_now true
或者通过其他方法


文档:,

我的建议有帮助吗?还有其他问题需要解决吗?