Jump to content

Problem with sending an object using REST API


Go to solution Solved by TheAccountant,

Recommended Posts

Hello,

I am trying to post new record into a database I created.

Could contain: File, Webpage, Page, Text

Documentation says I have to pass it as an object of key:value.

I copied the response when I tried to use GET to receive all the records and made the requests like this using Postman:

Could contain: Page, Text, File, Computer Hardware, Electronics, Hardware, Monitor, Screen

This is what is being stored in the database and I receive:

"title": "i", This should be Subject

"fields": {
        "field_12": "i", This is the title
        "field_13": "e", This should be Text
        "field_14": "l",
        "field_15": "d",
        "field_16": "_",
        "field_17": "1",
        "field_18": "2"
    },

It is somehow braking my text and fill all the field I have with parts of it.

Any idea why?

 

 

 

On the side, What I am trying to do is make like a Form Submit page in my community for applications.

I made it as Database to manage it there. First I tried to hide showing the content using groups permissions, but this way the Add New won't be seen by members and if I gave them the ability to see their own posts it will not be like a Form style. That is why I decided to hide everything and make a form myself and submit the data using the API.

If you have an advice for this as well, I really could use the help.

Link to comment
Share on other sites

  • Solution

I was able to solve it by making it in PHP and pass the whole information in one array. Before I was trying to link the form directly to API.

The best way I managed to handle it is by making the form send its request to a script I wrote and then send the information to API.

First collect all the fields of the information (fields object ) and then create an array to put all required fields from API along with field object it self.

 

This is my code:

First you prepare the setup of your API

$communityUrl = 'https://www.domain.com/';
$apiKey = '00000000000000000000000000000000';
$endpoint = '/cms/records/7'; // 7 is my database id

Then you receive the submitted data, handle it and store it in one associated array by making the key same is field id of each one of them. Try to make sure that field assigned as "Title" and "Content" are there.

$fields['28'] = $_POST['fullname']; // This is assigned as title
$fields['32'] = $_POST['age'];
$fields['29'] = $_POST['experience']; // this is assigned as content
$fields['35'] = $_POST['previousjob'];
$fields['33'] = $_POST['worklocations'];
$fields['34'] = $_POST['clients'];
$fields['30'] = $_POST['whatsapp'];
$fields['31'] = $_POST['email'];


$field['FIELD_ID_FROM_YOUR_DATABASE_HERE'] = ...

Then you put everything together in one new array ( required fields from API + fields object )

$data = array(
	"author" => AUTHOT_ID_FROM_YOUR_COMMUNITY,
	"category" => CATEGORY_ID_FROM_YOUR_DATABASE, 
"fields" => $fields, // Our field object
);

Finally you send your request to API.

$curl = curl_init( $communityUrl . 'api' . $endpoint );
curl_setopt_array( $curl, array(
  CURLOPT_RETURNTRANSFER	=> TRUE,
  CURLOPT_HTTPAUTH	=> CURLAUTH_BASIC,
  CURLOPT_USERPWD		=> "{$apiKey}:",
  CURLOPT_USERAGENT		=> "MyUserAgent/1.0",
  CURLOPT_POSTFIELDS => http_build_query($data),
) );
$response = curl_exec( $curl );
curl_close($curl);
if($response){
	echo 'Ok';
	exit;
}else{
	echo 'Error';
	exit;
}

 

Link to comment
Share on other sites

  • Recently Browsing   0 members

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