Thursday, October 27, 2022

[SOLVED] Inserting new event into CalDAV calendar using curl

Issue

I know how to use curl to insert new events into a CalDAV calendar, if the event is saved as a local ics file by a previous command:

$command > $localfile
curl -T "$localfile" $url

That works perfectly, but I'd like to do this without the need for a local file, I want to pipe the result from $command into curl, something like this:

$command | curl -T - $url

According to curl’s manual, the option "T -" should be what is required: it is supposed to read from stdin instead from a file. But if I use that option, I get this response:

<?xml version="1.0" encoding="utf-8"?>
    <d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">
      <s:exception>Sabre\DAV\Exception\Conflict</s:exception>
      <s:message>PUT is not allowed on non-files.</s:message>
    </d:error>

I've tried different other parameter like "--data" and "--data-raw", but I haven't found the right combination yet. Could someone help me with this, please?


Actually, the complete command cycle I am trying to realize looks like this:

cat<<EOC | curl -T - $url 
BEGIN:VCALENDAR
VERSION:2.0

BEGIN:VEVENT
CREATED:${CREATED}
UID:${RNDUID}
CLASS:PUBLIC
CATEGORIES:TV
SUMMARY:${SUMMARY}
LOCATION:${LOCATION}
DTSTART:${DTSTART}
DTEND:${DTEND}
DESCRIPTION:${DESCRIPTION}
END:VEVENT

END:VCALENDAR
EOC

I tried "echo" instead of "cat", but that doesn't change the result.


Solution

Eureka! :)

The solution is to make use of a HERE document:

$CURL $url -T /dev/stdin <<-EOF
        BEGIN:VCALENDAR
        VERSION:2.0
        BEGIN:VEVENT
        CREATED:$CREATED
        UID:$RNDUID
        SUMMARY:$SUMMARY
        LOCATION:$LOCATION
        DTSTART:$DTSTART
        DTSTAMP:$DTSTART
        DTEND:$DTEND
        DESCRIPTION:$DESCRIPTION
        END:VEVENT
        END:VCALENDAR
EOF

Regard the input parameter "/dev/stdin"! The curl parameter "-T" does not work there, when "-" (dash) is used for the input. You have to use "/dev/stdin" instead, which, sadly, is not available on all Unix like systems. On Linux it can be used as a replacement for "-", when programs don't support the option "-" or if the dash has a different meaning to them, like marking the end of all input parameters.

The "-" after "<<" is part of the HERE document command: it suppresses tabulators, but no blanks, so the document can be arranged in a more eye-friendly way.

I haven't figured out yet what's the difference between

cat<<EOF | curl ...
...
EOF

and the solution I found now, other than this first attempt clearly is a useless use of cat. But it looks very much like the "cat" brings in something that the old dog named "curl" doesn't like much. ;)

If some one has an explanation to this, I'd be very thankful to here it.



Answered By - weka
Answer Checked By - Willingham (WPSolving Volunteer)