我必须在我的应用程序中插入许多数据,通过图形界面需要很多时间。出于这个原因,我希望创建一个bash脚本,并使用REST通过curl发出请求(我必须手动指定id)。
问题是我得到了错误:服务器拒绝了这个请求,因为请求实体是被请求的方法的资源所不支持的格式。
这是代码
#!/bin/bash
for i in {1..1}
do
CURL='/usr/bin/curl -X POST'
RVMHTTP="http://192.168.1.101:8080/sitewhere/api/devices
-H 'accept:application/json'
-H 'content-type:application/json'
-H 'x-sitewhere-tenant:sitewhere1234567890'
--user admin:password"
DATA=" -d '{\"hardwareId":\"$i",\"siteToken\":\"4e6913db-c8d3-4e45-9436-f0a99b502d3c\",\"specificationToken\":\"82043707-9e3d-441f-bdcc-33cf0f4f7260\"}'"
# or you can redirect it into a file:
$CURL $RVMHTTP $DATA >> /home/bluedragon/Desktop/tokens
done我的请求格式必须是json。
发布于 2017-08-29 21:03:17
#!/usr/bin/env bash
rvmcurl() {
local url
url="http://192.168.1.101:8080/sitewhere/${1#/}"
shift || return # function should fail if we weren't passed at least one argument
curl -XPOST "${rvm_curl_args[@]}" "$url" "$@"
}
i=1 # for testing purposes
rvm_curl_args=(
-H 'accept:application/json'
-H 'content-type:application/json'
-H 'x-sitewhere-tenant:sitewhere1234567890'
--user admin:password
)
data=$(jq -n --arg hardwareId "$i" '
{
"hardwareId": $hardwareId,
"siteToken": "4e6913db-c8d3-4e45-9436-f0a99b502d3c",
"specializationToken": "82043707-9e3d-441f-bdcc-33cf0f4f7260"
}')
rvmcurl /api/devices -d "$data"注意:
eval (它携带自己的严重风险和警告)解析,则变成文字值。有关详细解释,请参见BashFAQ #50。jq )来确保生成的数据是合法的JSON。curl的包装器)。https://stackoverflow.com/questions/45948172
复制相似问题