【问题标题】:Loop two different CURL requets?循环两个不同的 CURL 请求?
【发布时间】:2019-10-02 19:48:17
【问题描述】:

我正在设置一个 shell 脚本来为我处理 curl 请求。

API 需要一个 OPTION 请求,然后我才能使用我的数据参数循环所有以下 POST 请求。

有没有人知道我可以如何改进我的代码和语法,以使 OPTIONS 请求发生一次,然后循环 POST 请求。

提前感谢您的任何指点。

我查看了有关 SH 的不同文档并浏览了很多 SO 页面,但到目前为止没有运气

#!/bin/bash
url="https://www.google.com"
    options="$(curl -s -X OPTIONS "$url"-H "Accept: */*"  -H "Access-Control-Request-Headers: authorization,content-type" -H "Access-Control-Request-Method: POST" -H "DNT: 1" -H "Origin: https://google.com" -H "User-Agent: Generic" -H "cache-control: no-cache")"
for options 
        do echo "$options" >> outputposts1.csv 2>&1
done
            for id in $(< idt.txt); 
                    do
                    posts="$(curl -v -s -m 5 -X POST -H "Accept: */*"  -H "Authorization: long.auth.key"  -H "Content-Type: application/json"  -H "DNT: 1"  -H "Origin: https://google.com"  -H "User-Agent: Generic"  -H "cache-control: no-cache" "$url" -d '{"profileid": "'"$id"'","content": "json content","programId": "numbers" \n')"
                    echo "$posts" >> outputposts2.csv 2>&1 
                    sleep 1s
done

OPTIONS 请求应提供 200 状态,以下 POST 请求应提供 201 状态代码。

【问题讨论】:

  • 没有我编辑了代码谢谢
  • 我想我修好了

标签: shell curl sh git-bash


【解决方案1】:

我自己解决了这个问题 基本上我用 shellcheck 给了我语法错误的指针,发现我在 "$(curl)" 中有一个 json 括号没有正确关闭并发现以下语法

#!/bin/bash
url="https://www.google.com"
    options="$(curl -s -X OPTIONS "$url"-H "Accept: */*")"
for options 
        do echo "$options"
done
            for id in $(< idt.txt); 
                    do
                    posts="$(curl -v -s -m 5 -X POST -H "Accept: */*" "$url" -d '{"profileid": "'"$id"'","content": "json content","programId": "numbers"}')
                    echo "$posts"
                    sleep 1s
done

【讨论】:

  • for optionsfor options in "$@" 的缩写。如果您的脚本实际上有任何位置参数,则该循环将覆盖您从调用 curl 获得的任何响应。
猜你喜欢
  • 2023-03-21
  • 1970-01-01
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
  • 2021-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多