2

We are trying to update a contact via REST API. But instead of submitting the changed values as seperate post parameters (e.g. "nick_name")

http://d46.demo.civicrm.org/sites/all/modules/civicrm/extern/rest.php?api_key=myscretapikey&key=sitekey&json=1&version=3&contact_id=2&entity=Contact&action=create&nick_name=Kathy

we want to submit the whole contact JSON object with its changed values as part of the request body:

POST http://d46.demo.civicrm.org/sites/all/modules/civicrm/extern/rest.php?api_key=myscretapikey&key=sitekey&json=1&version=3&contact_id=2&entity=Contact&action=create
Content-Type: application/json 
{ "id":"2", "contact_type":"Individual", "contact_sub_type":"", "do_not_email":"0", "do_not_phone":"0", "do_not_mail":"0", "do_not_sms":"0", "do_not_trade":"0", "is_opt_out":"0", "legal_identifier":"", "external_identifier":"", "sort_name":"Adams, Kathleen", "display_name":"Kathleen Adams", "nick_name":"Kathy", "legal_name":"", "image_URL":"", "preferred_communication_method":["3"], "preferred_language":"", "preferred_mail_format":"Both", "hash":"163700750", "api_key":"", "first_name":"Kathleen", "middle_name":"", "last_name":"Adams", "prefix_id":"", "suffix_id":"", "formal_title":"", "communication_style_id":"", "email_greeting_id":"1", "email_greeting_custom":"", "email_greeting_display":"Dear Kathleen", "postal_greeting_id":"1", "postal_greeting_custom":"", "postal_greeting_display":"Dear Kathleen", "addressee_id":"1", "addressee_custom":"", "addressee_display":"Kathleen Adams", "job_title":"", "gender_id":"1", "birth_date":"1979-03-17", "is_deceased":"0", "deceased_date":"", "household_name":"", "primary_contact_id":"", "organization_name":"", "sic_code":"", "user_unique_id":"", "created_date":"", "modified_date":"2015-05-14 06:03:56"}

Mostly this seems to work but we have some issues. Is this an official way to update a contact? And if not, which would be a better one?

4

Please, do not put the api_key nor key into the get parameter, to they don't end up in logs and other places, they are better as a post param.

As for your question, you can have json="{'id':2..."} as a param, ie json encode the whole contact and put as as a string param into a (post) json param

You might be safer to return only the modified param, as some are likely to be ignored (eg. the modification_date) or might expect a different format when modified than read (less and less a problem, but you might bump into edge cases still)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.