使用bash脚本执行PUT

使用bash脚本执行PUT,bash,rest,curl,Bash,Rest,Curl,我正在编写一个bash脚本来实现以下功能: 获取客户名单 对于每个客户,查询一个API(获取文件),该API返回id和名称 对于每个文件,执行PUT 我的问题是,我想在我的PUT body中同时包含“id”和“name”,那么如何在“files”API的单个循环中获得它呢。我写过这样的话: URL="https://some-url.com" API_TOKEN="some-token" get_customers() { curl --ins

我正在编写一个bash脚本来实现以下功能:

  • 获取客户名单
  • 对于每个客户,查询一个API(获取文件),该API返回id和名称
  • 对于每个文件,执行PUT
  • 我的问题是,我想在我的PUT body中同时包含“id”和“name”,那么如何在“files”API的单个循环中获得它呢。我写过这样的话:

    URL="https://some-url.com"
    API_TOKEN="some-token"
    
    
    get_customers() {
        curl --insecure \
             --fail \
             --header "Accept: application/json" \
             --header "Authorization: Bearer ${API_TOKEN}" \
             -k ${URL}/api/org/customers
    }
    
    get_files() {
        curl --insecure \
             --fail \
             --header "Accept: application/json" \
             --header "Authorization: Bearer ${API_TOKEN}" \
             -k ${URL}/api/files/files
    }
    
    put_capability() {
        echo ""
        curl --insecure \
             --fail \
             --silent \
             --header "Content-Type: application/json" \
             --header "Authorization: Bearer ${API_TOKEN}" \
             --data @- \
             ${URL}/api/files/files/{id}
        echo ""
    }
    
    
    customers=($(get_customers | jq --raw-output '.values[].id'))
    for customer_id in "${customers[@]}"; do
        fileshares=($(get_files ${customer_id} | jq --raw-output '.values[].id'))
        fileshares-names=($(get_files ${customer_id} | jq --raw-output '.values[].name'))
        for fileshare_id in "${fileshares[@]}"; do
            #call function put_capability and do subsequent PUT.
            #PUT needs two parameters, fileshares and fileshares-names
            
        done
    done
    

    基本上,我不想像现在这样两次调用“get_files”。我在随后的PUT API中需要“文件共享”和“文件共享名称”。这可以在文件共享的一个for循环中完成吗?

    您可以通过将其存储在一个临时文件中来解析
    获取\u文件
    输出一次,如下所示:

    customers=($(get_customers | jq--原始输出'.values[].id'))
    对于“${customers[@]}”中的客户标识;做
    获取文件${customer\u id}>/tmp/cust.txt
    文件共享=($(cat/tmp/cust.txt | jq--原始输出'.values[].id'))
    文件共享名称=($(cat/tmp/cust.txt | jq--原始输出'.values[].name'))
    对于“${fileshares[@]}”中的fileshare_id;做
    #调用函数put_功能并执行后续put。
    #PUT需要两个参数,fileshares和fileshares名称
    完成
    完成
    
    谢谢。我想你是在问我b/w JSON或XML。它支持JSON