将JSON文件卷曲到REST API,同时也使用--data-urlencode编码查询参数 [英] curl post JSON file to REST API while also using --data-urlencode to encode query paramater

查看:240
本文介绍了将JSON文件卷曲到REST API,同时也使用--data-urlencode编码查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置

在Bash脚本中,我正在使用curl将JSON文件的内容发布到在nginx后面的tomcat上运行的RESTful API.

In a Bash script I'm Using curl to POST the contents of a JSON file to a RESTful API running on tomcat behind nginx.

此POST还要求在URL末尾具有3个不同查询参数的基本身份验证.

This POST also requires basic auth with 3 different query parameters on the end of the URL.

while IFS= read -d '' -r file; do
 base=$(basename "$file")
 datetime=$(find $file -maxdepth 0 -printf "%TY/%Tm/%Td %TH:%TM:%.2TS")
 username="vangeeij"
 curl -vX POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" \
   -u username:password \
   -d @"$file" \
   "http://192.168.105.10/homeaccess/services/aCStats/uploadData?username=$username&filename=$base" \
 --data-urlencode datetime=$datetime

 #sudo mv "$file" /home/vangeeij/acserver/resultsOld
done < <(sudo find . -type f -print0)

结果/问题

从curl的输出中可以看出,URL的末尾没有附加datetime =参数

as can be seen in the output from curl, the URL does NOT have the datetime= parameter attached tot he end of the URL

* Connected to 192.168.105.10 (192.168.105.10) port 80 (#0)
* Server auth using Basic with user 'username'
> POST /homeaccess/services/aCStats/uploadData?username=username&filename=2017_3_1_8_50_RACE.json HTTP/1.1
> Host: 192.168.105.10

问题

完成我在这里尝试的操作的正确语法是什么?将JSON文件的POST卷曲到URL上,并在URL上附加参数,至少其中之一要进行URL编码?

What is the proper syntax to accomplish what I'm trying here. Curl POST of JSON file to URL with the attachment of parameters to the URL, at least one of which HAS to be URLEncoded?

推荐答案

解决方案

我发现处理URL编码字符串并将其作为参数传递给URL的过程的最简单方法是如下修改我的脚本.

I found the simplest way to handle the process of URLEncoding a string and passing it as a parameter in the URL was to modify my script as following.

#!/bin/bash
cd /home/vangeeij/acserver/results

urlencode() {
   # urlencode <string>
   old_lc_collate=$LC_COLLATE
   LC_COLLATE=C

   local length="${#1}"
   for (( i = 0; i < length; i++ )); do
       local c="${1:i:1}"
       case $c in
           [a-zA-Z0-9.~_-]) printf "$c" ;;
           *) printf '%%%02X' "'$c" ;;
       esac
   done

   LC_COLLATE=$old_lc_collate
}


while IFS= read -d '' -r file; do
   base=$(basename "$file")
   datetime=$(find $file -maxdepth 0 -printf "%TY/%Tm/%Td %TH:%TM:%.2TS")
   datetimeEncoded=$(urlencode "$datetime")
username="username"
   curl -vX POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" \
      -u username:password \
      -d @"$file" \
       "http://192.168.105.10/homeaccess/services/aCStats/uploadData?username=$username&filename=$base&datetime=$datetimeEncoded"
   sudo mv "$file" /home/vangeeij/acserver/resultsOld
 done < <(sudo find . -type f -print0)

简短回答

我添加了函数"urlencode()".然后将字符串变量$ datetime传递给它,以根据需要对日期时间进行编码.然后将结果字符串变量$ datetimeEncoded传递给URL参数datetime = $ datetimeEncoded

I added the function "urlencode()". Then pass the String variable $datetime through it to encode the date time as needed.. then pass the resulting String variable $datetimeEncoded to the URL parameter datetime=$datetimeEncoded

问题解决了.

在此处找到URLEncode脚本

这篇关于将JSON文件卷曲到REST API,同时也使用--data-urlencode编码查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆