我必须在我的应用程序中插入许多数据,并且通过图形界面需要很多时间。出于这个原因,我想创建一个 bash 脚本并使用 REST API 通过 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
Run Code Online (Sandbox Code Playgroud)
我的请求格式必须是 json
#!/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"
Run Code Online (Sandbox Code Playgroud)
笔记:
eval,会带来严重的风险和警告)成为字面值。有关完整说明,请参阅BashFAQ #50。jq)来确保生成的数据是合法的 JSON。curl在 PATH、导出的 shell 函数或其他情况下安装包装器)。| 归档时间: |
|
| 查看次数: |
9790 次 |
| 最近记录: |