卷曲:无法使用riak解析主机

时间:2017-11-13 11:48:58

标签: json bash curl riak

我有这个bash脚本,我用来读取csv文件并在riak中插入我的数据。

#!/bin/bash
# Set "," as the field separator using $IFS 
# and read line by line using while read combo 
while IFS=',' read -r Num_Acc senc catv occutc obs obsm choc manv num_veh
do
eval curl -i -X POST http://127.0.0.1:8098/riak/vehicule -H "Content-Type:application/json" -d {"Num_Acc":"$Num_Acc","senc":$senc,"catv":"$catv","occutc":"$occutc","obs":"$obs","obsm":$obsm,"choc":$choc,"manv":"$manv","num_veh":"$num_veh"}
done < vehicules_2016.csv

问题是我有这个错误,我不知道它可能来自哪里:

curl: (6) Could not resolve host: senc
curl: (6) Could not resolve host: catv
curl: (6) Could not resolve host: occutc
curl: (6) Could not resolve host: obs
curl: (6) Could not resolve host: obsm
curl: (6) Could not resolve host: choc
curl: (6) Could not resolve host: manv
curl: (3) Illegal characters found in URL
HTTP/1.1 201 Created
Vary: Accept-Encoding
Server: MochiWeb/1.1 WebMachine/1.10.9 (cafe not found)
Location: /riak/vehicule/NiuPBkgCFBaqUNhorovnBvix2ED
Date: Mon, 13 Nov 2017 11:43:08 GMT
Content-Type: application/json
Content-Length: 0

我认为问题可能是json语法。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您必须在双引号"和转义内部双引号(如\")之间附加数据,或者您也可以使用heredoc

#!/bin/bash
while IFS=',' read -r Num_Acc senc catv occutc obs obsm choc manv num_veh
do
data=$(cat <<EOF
{
    "Num_Acc":"$Num_Acc",
    "senc": "$senc",
    "catv": "$catv",
    "occutc": "$occutc",
    "obs": "$obs",
    "obsm": "$obsm",
    "choc": "$choc",
    "manv": "$manv",
    "num_veh": "$num_veh"
}
EOF
)

curl -i http://127.0.0.1:8098/riak/vehicule \
     -H "Content-Type: application/json" \
     -d "$data"

done < vehicules_2016.csv

检查this post