2

Problem:

I am attempting to use my CiviCRM instance's API v4 REST endpoint to get and post data. I eventually want to script tasks like importing new contacts and event registrations using Python, but first I am simply trying to verify that I can GET/POST data. Using the API v4 Explorer, I crafted a very simple query: get a contact and return ID, first name, and last name, where ID = 15. When I execute in the explorer, I get the expected response:

  {
    "id": 15,
    "contact_id.first_name": "Jane",
    "contact_id.last_name": "Doe"
  }
] 

However, when I use the suggested Curl command (in zsh):

CRM_URL='https://mywebsite.org/wp-admin/admin.php?page=CiviCRM&q=civicrm%2Fajax%2Fapi4%2FActivityContact%2Fget'
CRM_AUTH=''X-Civi-Auth: Bearer MY_API_KEY' 

# Note: 'MY_API_KEY' is actually a valid API key

curl -X POST -H "$CRM_AUTH" "$CRM_URL" \
-d 'params=%7B%22select%22%3A%5B%22id%22%2C%22contact_id.first_name%22%2C%22contact_id.last_name%22%5D%2C%22where%22%3A%5B%5B%22id%22%2C%22%3D%22%2C15%5D%5D%2C%22limit%22%3A25%7D'

I get nothing in response. When I add the -v flag to curl to get more info, I get an interesting response (see "curl response" below). It appears that I have made a valid connection, but am getting no data (JSON or otherwise) back.

Note that a similar issue occurs when I try to execute this command in Python using the Requests library (the response returns a 200 status code, but no content).

What is going on here? Why am I not getting the response I am getting in the API Explorer?

Curl Response

This is the response I get (in zsh) when executing the curl command above:

*   Trying <Redacted IP>:443...
* Connected to mywebsite.org (<Redacted IP>) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*  CAfile: /etc/ssl/cert.pem
*  CApath: none
* (304) (OUT), TLS handshake, Client hello (1):
* (304) (IN), TLS handshake, Server hello (2):
* (304) (OUT), TLS handshake, Client hello (1):
* (304) (IN), TLS handshake, Server hello (2):
* (304) (IN), TLS handshake, Unknown (8):
* (304) (IN), TLS handshake, Certificate (11):
* (304) (IN), TLS handshake, CERT verify (15):
* (304) (IN), TLS handshake, Finished (20):
* (304) (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / AEAD-AES256-GCM-SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: CN=*.mywebsite.org
*  start date: Jun  3 01:31:35 2022 GMT
*  expire date: Sep  1 01:31:34 2022 GMT
*  subjectAltName: host "mywebsite.org" matched cert's "mywebsite.org"
*  issuer: C=US; O=Let's Encrypt; CN=R3
*  SSL certificate verify ok.
* Using HTTP2, server supports multiplexing
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x<redacted>)
> POST /wp-admin/admin.php?page=CiviCRM&q=civicrm%2Fajax%2Fapi4%2FContact%2Fget HTTP/2
> Host: mywebsite.org
> user-agent: curl/7.79.1
> accept: */*
> x-civi-auth: Bearer <Redacted API Key>
> content-length: <Redacted>
> content-type: application/x-www-form-urlencoded
> 
* We are completely uploaded and fine
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
< HTTP/2 302 
< server: nginx
< date: Wed, 29 Jun 2022 00:30:03 GMT
< content-type: text/html; charset=UTF-8
< content-length: 0
< location: https://mywebsite.org/wp-login.php?redirect_to=https%3A%2F%2Fmywebsite.org%2Fwp-admin%2Fadmin.php%3Fpage%3DCiviCRM%26q%3Dcivicrm%252Fajax%252Fapi4%252FContact%252Fget&reauth=1
< set-cookie: PHPSESSID=<Redacted>; path=/
< expires: Wed, 11 Jan 1984 05:00:00 GMT
< cache-control: no-cache, must-revalidate, max-age=0
< pragma: no-cache
< x-redirect-by: WordPress
< x-httpd-modphp: 1
< host-header: <Redacted>
< x-proxy-cache-info: DT:1
< 
* Connection #0 to host mywebsite.org left intact

Further debugging info:

  • WordPress version: 6.0
  • CiviCRM version: 5.50.1
  • AuthX version: 5.50.1
  • curl version: 7.79.1
  • PHP version: 7.4.30

Thanks in advance!

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.