通过 bash 脚本执行 curl 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45948172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:25:22  来源:igfitidea点击:

executing a curl request through bash script

bashshellcurl

提问by Mauro Cruz

I have to insert many data in my application and through the graphical interface it takes many time. For this reason I want to create a bash script and make the requests through curl using the REST API (I have to manually specify the id).

我必须在我的应用程序中插入许多数据,并且通过图形界面需要很多时间。出于这个原因,我想创建一个 bash 脚本并使用 REST API 通过 curl 发出请求(我必须手动指定 id)。

The problem is that i get the error: The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

问题是我收到错误:服务器拒绝此请求,因为请求实体的格式不受所请求方法的请求资源支持。

Here is the code

这是代码

#!/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

The format of my request has to be json

我的请求格式必须是 json

回答by Charles Duffy

#!/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"

Note:

笔记:

  • Commands, or command fragments intended to be parsed into multiple words, should neverbe stored in strings. Use an array or a function instead. Quotes inside such strings are not parsed as syntax, and instead (when parsed without eval, which carries its own serious risks and caveats) become literal values. See BashFAQ #50for a full explanation.
  • Use a JSON-aware tool, such as jq, to ensure that generated data is legit JSON.
  • Fully-qualifying paths to binaries is, in general, an antipattern. It doesn't result in a significant performance gain (the shell caches PATH lookups), but it doesreduce your scripts' portability and flexibility (preventing you from installing a wrapper for curlin your PATH, in an exported shell function, or otherwise).
  • All-caps variable names are in a namespace used for variables with meaning to the shell and operating system. Use names with at least one lowercase character for your own variables to prevent any chance of conflict.
  • 命令,或意图被解析成多个字命令片段,应从未被存储在字符串。改用数组或函数。此类字符串中的引号不会被解析为语法,而是(当不使用 解析时eval,会带来严重的风险和警告)成为字面值。有关完整说明,请参阅BashFAQ #50
  • 使用支持 JSON 的工具(例如jq)来确保生成的数据是合法的 JSON。
  • 二进制文件的完全限定路径通常是一种反模式。它不会带来显着的性能提升(shell 缓存 PATH 查找),但它确实降低了脚本的可移植性和灵活性(阻止您curl在 PATH、导出的 shell 函数中或以其他方式安装包装器)。
  • 全大写的变量名位于用于对 shell 和操作系统有意义的变量的命名空间中。为您自己的变量使用至少包含一个小写字符的名称,以防止发生任何冲突。