Skip to content

Instantly share code, notes, and snippets.

@johnboxall
Created February 1, 2023 19:25
Show Gist options
  • Save johnboxall/6be38cb83490b5b9bc431e7398f55c32 to your computer and use it in GitHub Desktop.
Save johnboxall/6be38cb83490b5b9bc431e7398f55c32 to your computer and use it in GitHub Desktop.
How to interact with a Salesforce B2C Commerce instance over WebDAV
#!/bin/bash
# You can modify files on instance progmatically using the WebDAV protocol.
# First create an Account Manager API Client, noting its ID and secret.
# Then on your instance, grant that client access to the WebDAV API in WebDAV Client Permissions page.
# Finally, use the ID and secret to get an access token from Account Manager and use that to call WebDAV on your instance.
set -euo pipefail
HOST='YOUR-INSTANCE-HOSTNAME' # eg. zzrf-001.dx.commercecloud.salesforce.com
CLIENT='YOUR-AM-CLIENT-ID'
SECRET='YOUR-AM-CLIENT-SECRET'
echo '--> Get a AM token for the client...'
BASE_AM='https://account.demandware.com'
TOKEN=$(curl "$BASE_AM/dwsso/oauth2/access_token" \
-sSfu "$CLIENT:$SECRET" \
-d 'grant_type=client_credentials' \
| jq -r '.access_token')
echo '--> List files...'
curl "https://$HOST/on/demandware.servlet/webdav/Sites/Cartridges/version1/" \
-sSfH "Authorization: Bearer $TOKEN" | htmlq 'a' -a 'href'
echo '--> Upload a file...'
curl "https://$HOST/on/demandware.servlet/webdav/Sites/Cartridges/version1/" \
-X 'PUT' \
-H "Authorization: Bearer $TOKEN" \
-T "my_cartridge.zip"
echo '--> Unzip a file...'
curl "https://$HOST/on/demandware.servlet/webdav/Sites/Cartridges/version1/my_cartridge.zip" \
-sSfH "Authorization: Bearer $TOKEN" \
-d 'method=UNZIP'
echo '--> Delete a file...'
curl "https://$HOST/on/demandware.servlet/webdav/Sites/Cartridges/version1/my_cartridge.zip" \
-sSfX 'DELETE' \
-H "Authorization: Bearer $TOKEN"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment