Search code examples
google-apicarddav

How to insert a new contact using Google CardDAV API?


Google CardDAV API Developer's Guide is very brief when it comes to contact manipulation. The only hint about how to insert a new contact is this:

Client applications issue a POST request with the new contact in VCard 3.0 format. The response will contain the ID of the new contact.

This is the request I issue, but the response I get is 400 Bad Request:

POST https://www.googleapis.com/carddav/v1/principals/[email protected]/lists/default/ HTTP/1.1
Authorization: Bearer ya29.foobar
Content-Type: text/vcard; charset="UTF-8"
Host: www.googleapis.com
Content-Length: 77
Expect: 100-continue
Connection: Keep-Alive

BEGIN:VCARD
VERSION:3.0
FN:Foo Bar
UID:abc-def-fez-1234546578
END:VCARD

Anybody has a clue what am I missing here?


Solution

  • Do a PUT, not a POST. Like this:

    PUT /carddav/v1/principals/[email protected]/lists/default/DEADBEEF.vcf
    If-None-Match: *
    Authorization: ...
    Host: www.googleapis.com
    Content-Type: text/vcard; charset="UTF-8"
    Expect: 100-continue
    
    BEGIN:VCARD
    VERSION:3.0
    PRODID:-//YouYou//Windows//EN
    FN:Foo Bar
    N:Bar;Foo;;;
    UID:DEADBEEF
    END:VCARD