如何通过curl

时间:2018-05-22 15:30:54

标签: rest api asynchronous curl confluence

我有一个curl命令,使用K15t Scroll Version插件发布Atlassian Confluence空间:

curl -v -H "Accept:application/json" -H "Content-Type:application/json" -X POST "http://<server>/rest/scroll-versions/1.0/publish/<spacekey>/to_new/publish?os_username=myusername&os_password=mypassword" --data {"versionId":"9C8CC11601610D3E794E334A746E61SD","targetSpaceKey":"pro60", "targetSpaceName":"Pro 60", "treeProviderName":"Pro 2017", "languageKey":"all", "copyLabels":"false", "keepAuthorsAndDates":"false", "removeAttachments":"false", "sendEmail":"false", "keepPermalinks":"false", "onlyPagesInFinalState":"false", "onlyPagesInFinalStateProcessChildren":"false", "publishHierarchy":"false"}

此命令返回响应代码202,其中包含正在发生的事件的摘要。然后,我需要继续检查下面的URL,以了解活动何时完成。我一直得到代码202,最后,我得到了代码201.然后,我保存了我在下面调用URL的响应。

http://<server>/rest/scroll-versions/1.0/async-tasks/<alphanumeric id generated when 202 is returned> 

在执行curl后跟上面的URL调用后,我该怎么做才能保存对日志文件的响应?

1 个答案:

答案 0 :(得分:0)

wait-for-code.sh(基于https://superuser.com/a/442395/634271

#/bin/sh

HOST=${1}
ATTEMPTS=${2:-10} # -1 for unlimited
CODE=${3:-200}
OUT=${4:-/dev/null}
DELAY=${5:-1s}

if [[ "$OUT" = "/dev/null" ]]; then
    GET_BODY="I"
else
    GET_BODY=""
fi

CURL="curl -${GET_BODY}so ${OUT} -w %{http_code} ${HOST}"

until [ $ATTEMPTS -eq 0 ]; do
    [ `$CURL` -eq $CODE ] && exit 0
    [ $ATTEMPTS -gt 0 ] && [ $((--ATTEMPTS)) ]
    [ $ATTEMPTS -gt 0 ] && sleep $DELAY
done

exit 1

您的情况下的用法:

$ SERVER="http://<server>/rest/scroll-versions/1.0/async-tasks/<alphanumeric id generated when 202 is returned>"
$ ./wait-for-http-code.sh $SERVER -1 201 out.log