使用curl执行多个请求,具有不同的选项

时间:2015-02-05 10:32:34

标签: bash sharepoint curl

我尝试使用curl将文件上传到sharepoint。我可以通过以下三个步骤成功完成此操作(即3个单独的curl调用以检查文件,上传并重新检入),使用以下帖子中的建议:

How to check-out a file from sharepoint document library using curl?

我的个人请求如下:

# Checkout the index.html file
curl --ntlm --user ${USER} \
    --data @- \
    -H "SOAPAction: http://schemas.microsoft.com/sharepoint/soap/CheckOutFile" \
    -H "Content-Type: text/xml; charset=utf-8" \
    ${SHAREPOINT}/_vti_bin/Lists.asmx << EOF
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <CheckOutFile xmlns="http://schemas.microsoft.com/sharepoint/soap/">
      <pageUrl>${FILE}</pageUrl>
      <checkoutToLocal>false</checkoutToLocal>
      <lastmodified/>
    </CheckOutFile>
  </soap:Body>
</soap:Envelope>
EOF

# upload the file
curl --ntlm -u ${USER} \
    -T HTML/2015/index.html \
    ${FOLDER}

curl --ntlm --user ${USER} \
    --data @- \
    -H "SOAPAction: http://schemas.microsoft.com/sharepoint/soap/CheckInFile" \
    -H "Content-Type: text/xml; charset=utf-8" \
    ${SHAREPOINT}/_vti_bin/Lists.asmx << EOF
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <CheckInFile xmlns="http://schemas.microsoft.com/sharepoint/soap/">
      <pageUrl>${FILE}</pageUrl>
      <comment>Automagic update</comment>
      <checkinType>0</checkinType>
    </CheckInFile>
  </soap:Body>
</soap:Envelope>
EOF

不幸的是,这导致cUrl要求我输入密码3次(这是一个很长的密码!:-))。我也不喜欢.netrc文件的想法,因为将密码写入磁盘并不是一个好主意。

因此,我认为我可以做的是将所有请求合并到一个命令行中,根据需要设置和删除标头,使用bash进程替换等提供请求主体等。

curl --ntlm --user ${USER} \
    --trace-ascii publish.log \
    --data @<(echo "$CHECKOUT") \
    -H "SOAPAction: http://schemas.microsoft.com/sharepoint/soap/CheckOutFile" \
    -H "Content-Type: text/xml; charset=utf-8" \
    ${SHAREPOINT}/_vti_bin/Lists.asmx \
    -H "SOAPAction:" \
    -H "Content-Type:" \
    -T HTML/2015/index.html \
    ${FOLDER} \
    --data @<(echo "$CHECKIN") \
    -H "SOAPAction: http://schemas.microsoft.com/sharepoint/soap/CheckInFile" \
    -H "Content-Type: text/xml; charset=utf-8" \
    ${SHAREPOINT}/_vti_bin/Lists.asmx

不幸的是,发生的情况是cUrl似乎一次处理所有选项,然后才尝试请求URL,导致一个URL覆盖选项,另一个URL,最终没有任何工作。日志文件中的一个片段:

> 0000: PUT /xxx/xxx/_vti_bin/Lists.asmx HTTP/1.1
> 0033: Authorization: NTLM AAAAAAAAAAA=
> 0075: User-Agent: curl/7.30.0
> 008e: Host: example.com
> 00a8: Accept: */*
> 00b5: SOAPAction: http://schemas.microsoft.com/sharepoint/soap/CheckOu
> 00f5: tFile
> 00fc: Content-Type: text/xml; charset=utf-8
> 0123: SOAPAction: http://schemas.microsoft.com/sharepoint/soap/CheckIn
> 0163: File
> 0169: Content-Type: text/xml; charset=utf-8
> 0190: Content-Length: 0
> 01a3: Expect: 100-continue

注意重复的SOAPAction标头,而我希望只应用第一个选项。

有没有办法说&#34;现在停止处理选项,执行此URL,然后继续&#34;?

1 个答案:

答案 0 :(得分:0)

如果输入密码三次是唯一的问题,您可以提示输入密码并在变量中读取密码并在curl命令中使用它,如下所示。

echo "Password: "
read -s PASSWORD
# Checkout the index.html file
curl --ntlm --user ${USER}:${PASSWORD} \
    --data @- \
    -H "SOAPAction: http://schemas.microsoft.com/sharepoint/soap/CheckOutFile" \
    -H "Content-Type: text/xml; charset=utf-8" \
    ${SHAREPOINT}/_vti_bin/Lists.asmx << EOF
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <CheckOutFile xmlns="http://schemas.microsoft.com/sharepoint/soap/">
      <pageUrl>${FILE}</pageUrl>
      <checkoutToLocal>false</checkoutToLocal>
      <lastmodified/>
    </CheckOutFile>
  </soap:Body>
</soap:Envelope>
EOF

# upload the file
curl --ntlm -u ${USER}:${PASSWORD} \
    -T HTML/2015/index.html \
    ${FOLDER}

curl --ntlm --user ${USER}:${PASSWORD} \
    --data @- \
    -H "SOAPAction: http://schemas.microsoft.com/sharepoint/soap/CheckInFile" \
    -H "Content-Type: text/xml; charset=utf-8" \
    ${SHAREPOINT}/_vti_bin/Lists.asmx << EOF
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <CheckInFile xmlns="http://schemas.microsoft.com/sharepoint/soap/">
      <pageUrl>${FILE}</pageUrl>
      <comment>Automagic update</comment>
      <checkinType>2</checkinType>
    </CheckInFile>
  </soap:Body>
</soap:Envelope>
EOF