Jump to content

REST API - How to send Arrays?


Recommended Posts

I am trying to edit the value of a users custom profile field via the REST API, but the documentation is as clear as mud to me.

This probably seems obvious to someone used to dealing with form data etc. But I have no idea how an array is sent in form data.

So I make a post request to this endpoint

POST https://example.com/api/core/members/4?key=<my_api_key>

What would the raw body look like if I wanted to say set customField ID 3 to 'Foo' and field 4 to  'Bar'?

Could someone just show me what the body would look like so I have some idea of what I'm aiming for here...

Edited by Roboko
Link to comment
Share on other sites

Hi @Roboko

Here's an example you can plug into a custom php block for testing purposes. Just update communityUrl, apiKey, and the specified member ID:

<?php
//REST API URL and KEY

$communityUrl = 'https://www.yourdomain.com/';
$apiKey = 'abcdefghijklmnopqrstuvwxyz123456';
$id = 1; //Member ID

// REST Endpoint
$endpoint = "/core/members/{$id}";
$curl = curl_init( $communityUrl . 'api' . $endpoint );

// POST data
$curl_post_data = array(
'customFields' => array( '3' => 'foo', '4' => 'bar' ),
);

// Prepare data for cURL POST
$curl_post_data = urldecode(http_build_query($curl_post_data)); //Turn parameter array into a string that looks like 'category=1&author=1&title=test file', etc.  Only non-null items are included in this list.  urldecode turns characters like %26 back into &.

// POST Data
curl_setopt_array( $curl, array(
CURLOPT_RETURNTRANSFER	=> TRUE,
CURLOPT_HTTPAUTH	=> CURLAUTH_BASIC,
CURLOPT_USERPWD		=> "{$apiKey}:",
CURLOPT_POST		=> TRUE,
CURLOPT_POSTFIELDS  => $curl_post_data

) );

// Get Response JSON
$response = curl_exec( $curl );

// Build object
$response = json_decode($response);

echo \IPS\Member::load($response->id)->url()->setQueryString( 'tab', 'field_core_pfield_3' );
echo \IPS\Member::load($response->id)->url()->setQueryString( 'tab', 'field_core_pfield_4' );

Hope it helps!

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...