Jump to content

REST API /cms/records/{databaseId} POST fails with 500 in 4.7.16


Recommended Posts

Was working on something new and started issuing POSTs to a Pages DB but it's erroring with a 500.  System log entry below.

Being this is a create of a record via a POST, I'm not sure why it is triggering an edit (requiring a record_id) which is also via a POST.

Could contain: Page, Text, File

Link to comment
Share on other sites

Posted (edited)
48 minutes ago, Jim M said:

Are you getting this with any post made to this endpoint or specifically with a certain request?

I tried another Pages DB and POST call that worked before the update and it too is getting the same error.

Edited by Clover13
Link to comment
Share on other sites

Posted (edited)

It's just a python script, I put this together to post to the default Articles Pages DB and get a 500 as well.

 def test_api_cms_post(self):

        post_cms_record_url = f"{self.API_URL}{self.CMS_RECORDS_ENDPOINT}1"
        logging.info(f"post_cms_record_url: {post_cms_record_url}")

        # Request parameters with API key included in the URL
        request_params = {
            'key': self.API_KEY
        }

        post_data = {'category': 1, 'author': 1, 'fields[1]': 'Title 123', 'fields[2]': 'Content ABC'}
        logging.info(f"post_data: {post_data}")

        # Make a POST request to the API endpoint
        response = requests.post(post_cms_record_url,  data=post_data, params=request_params)

        # Check if the request was successful (status code 200)
        if response.status_code == 200:
            # Parse the JSON response
            data = response.json()

            # Process the data as needed
            print("Response:", data)
        else:
            # Print an error message if the request was not successful
            print(f"Error: {response.status_code}")

Output:

2024-03-20 16:13:43 - INFO - post_cms_record_url: http://localhost/api/index.php?/cms/records/1
2024-03-20 16:13:43 - INFO - post_data: {'category': 1, 'author': 1, 'fields[1]': 'Title 123', 'fields[2]': 'Content ABC'}
2024-03-20 16:13:43 - DEBUG - Starting new HTTP connection (1): localhost:80
Error: 500
2024-03-20 16:13:43 - DEBUG - http://localhost:80 "POST /api/index.php?/cms/records/1&key=ef2f4f1687b901d3c962f074dcd4528e HTTP/1.1" 500 72


Ran 1 test in 0.140s

OK

 

Edited by Clover13
Link to comment
Share on other sites

500 error code aside, the API should also return a separate code/message about the issue. Have you tried checking that?

2T306/4	INVALID_DATABASE
1T306/5	NO_CATEGORY
1T306/6	NO_AUTHOR
2T306/G	NO_PERMISSION
1T306/D	TITLE_CONTENT_REQUIRED
1S306/E	UPLOAD_FIELD_NOT_OBJECT
1T306/F	UPLOAD_FIELD_NO_FILES
1T306/G	UPLOAD_FIELD_MULTIPLE_NOT_ALLOWED
1T306/H	UPLOAD_FIELD_IMAGE_NOT_SUPPORTED
1T306/I	UPLOAD_FIELD_IMAGES_ONLY
1T306/J	UPLOAD_FIELD_EXTENSION_NOT_ALLOWED
Link to comment
Share on other sites

8 minutes ago, teraßyte said:

500 error code aside, the API should also return a separate code/message about the issue. Have you tried checking that?

2T306/4	INVALID_DATABASE
1T306/5	NO_CATEGORY
1T306/6	NO_AUTHOR
2T306/G	NO_PERMISSION
1T306/D	TITLE_CONTENT_REQUIRED
1S306/E	UPLOAD_FIELD_NOT_OBJECT
1T306/F	UPLOAD_FIELD_NO_FILES
1T306/G	UPLOAD_FIELD_MULTIPLE_NOT_ALLOWED
1T306/H	UPLOAD_FIELD_IMAGE_NOT_SUPPORTED
1T306/I	UPLOAD_FIELD_IMAGES_ONLY
1T306/J	UPLOAD_FIELD_EXTENSION_NOT_ALLOWED

I dropped the data parsing for the non-200 case, here is that output:

 

2024-03-20 16:38:05 - INFO - post_cms_record_url: http://localhost/api/index.php?/cms/records/1
2024-03-20 16:38:05 - INFO - post_data: {'category': 1, 'author': 1, 'fields[1]': 'Title 123', 'fields[2]': 'Content ABC'}
2024-03-20 16:38:05 - DEBUG - Starting new HTTP connection (1): localhost:80
2024-03-20 16:38:05 - DEBUG - http://localhost:80 "POST /api/index.php?/cms/records/1&key=ef2f4f1687b901d3c962f074dcd4528e HTTP/1.1" 500 72


Ran 1 test in 0.179s

OK
Error: 500
Response: {'errorCode': 'EX1048', 'errorMessage': 'UNKNOWN_ERROR'}

Link to comment
Share on other sites

Posted (edited)
24 minutes ago, Adriano Faria said:

EX1048 means that a column cannot be NULL.

Right and I believe that's due to the first screenshot I posted where it's expecting a record_id when one doesn't exist yet since this is creating a record, not editing one.  Unless that DB update is made after the record is created (but not committed yet), I don't know the sequencing or transactionalization of their DB commands for the API.

Maybe it's due to the api key being a request param instead of a header, but the header approach wouldn't work for me before this current version of the software.

Edited by Clover13
Link to comment
Share on other sites

FWIW...

 

# With API Key in request params returns 500 and EX1048
response = requests.post(post_cms_record_url, data=post_data, params=request_params)

# With API Key base64 encoded in headers
# Error: 401
# Response: {'errorCode': '3S290/7', 'errorMessage': 'INVALID_API_KEY'}
response = requests.post(post_cms_record_url, data=post_data, headers=headers)

# With API Key in auth returns 500 and EX1048
response = requests.post(post_cms_record_url, data=post_data, auth=auth)

Link to comment
Share on other sites

In the first instance, try removing the 2 hooks you have in play there, and see if that resolves the problem. Im not able to replicate it on this end, and since you have this on local, its not too easy to debug

Link to comment
Share on other sites

4 hours ago, Marc Stridgen said:

In the first instance, try removing the 2 hooks you have in play there, and see if that resolves the problem. Im not able to replicate it on this end, and since you have this on local, its not too easy to debug

I'll do some more testing today.

FWIW, the GET to the hello endpoint works fine.

I'll also get appropriate errors when the POST executes and determines something like an invalid category or author.

2024-03-21 10:21:24 - INFO - post_cms_record_url: http://localhost/api/index.php?/cms/records/1
2024-03-21 10:21:24 - INFO - post_data: {'category': 99, 'author': 1, 'fields[1]': 'Title 123', 'fields[2]': 'Content ABC'}
2024-03-21 10:21:24 - DEBUG - Starting new HTTP connection (1): localhost:80
2024-03-21 10:21:24 - DEBUG - http://localhost:80 "POST /api/index.php?/cms/records/1&key=ef2f4f1687b901d3c962f074dcd4528e HTTP/1.1" 400 72


Ran 1 test in 0.094s

OK
Error: 400
Response: {'errorCode': '1T306/5', 'errorMessage': 'NO_CATEGORY'}

 

Regarding the hooks being disabled, that did not resolve the issue.

2024-03-21 10:25:57 - INFO - post_cms_record_url: http://localhost/api/index.php?/cms/records/1
2024-03-21 10:25:57 - INFO - post_data: {'category': 1, 'author': 1, 'fields[1]': 'Title 123', 'fields[2]': 'Content ABC'}
2024-03-21 10:25:57 - DEBUG - Starting new HTTP connection (1): localhost:80
Error: 500
Response: {'errorCode': 'EX1048', 'errorMessage': 'UNKNOWN_ERROR'}
2024-03-21 10:25:58 - DEBUG - http://localhost:80 "POST /api/index.php?/cms/records/1&key=ef2f4f1687b901d3c962f074dcd4528e HTTP/1.1" 500 72
 

Could contain: Page, Text, File

Link to comment
Share on other sites

Looking again at the screenshot, the error is being thrown when a revision for the record is added to the database, not when the record itself is added. If you're adding a new record, it shouldn't store a revision. A revision should be saved only when you edit a record.

 

The problem is in /applications/cms/api/records.php in the _createOrUpdate() function (lines 424-443):

			/* Store a revision before we change any values */
			if ( $item::database()->revisions )
			{
				$revision = new \IPS\cms\Records\Revisions;
				$revision->database_id = $item::$customDatabaseId;
				$revision->record_id   = $item->_id;
				$revision->data        = $item->fieldValues( TRUE );
				
				if ( $this->member )
				{
					$memberId = $this->member->member_id;
				}
				else
				{
					$memberId = $item->author()->member_id;
				}
				
				$revision->member_id = $memberId;
				$revision->save();
			}

 

The IF check should also check if you're editing a record because when adding a new one there is no record ID available yet (thus the column NULL error):

if ( $type == 'edit' AND $item::database()->revisions )
Edited by teraßyte
Link to comment
Share on other sites

4 minutes ago, teraßyte said:

Looking again at the screenshot, the error is being thrown when a revision for the record is added to the database, not when the record itself is added. If you're adding a new record, it shouldn't store a revision. A revision should be saved only when you edit a record.

 

The problem is in /applications/cms/api/records.php in the _createOrUpdate() function (lines 424-443):

			/* Store a revision before we change any values */
			if ( $item::database()->revisions )
			{
				$revision = new \IPS\cms\Records\Revisions;
				$revision->database_id = $item::$customDatabaseId;
				$revision->record_id   = $item->_id;
				$revision->data        = $item->fieldValues( TRUE );
				
				if ( $this->member )
				{
					$memberId = $this->member->member_id;
				}
				else
				{
					$memberId = $item->author()->member_id;
				}
				
				$revision->member_id = $memberId;
				$revision->save();
			}

 

The IF check should also check if you're editing a record because when adding a new one there is no record ID available yet (thus the column NULL error):

if ( $type == 'edit' AND $item::database()->revisions )

Thanks @teraßyte.  I noticed the edit aspect and had mentioned that earlier.  I would think any POST to create a new record would fail in this case (which is what I'm experiencing across 3 different Pages DBs), but @Marc Stridgen wasn't able to reproduce it.  @Marc Stridgen can you confirm you tested on 4.7.16?

Link to comment
Share on other sites

Thank you for bringing this issue to our attention! I can confirm this should be further reviewed and I have logged an internal bug report for our development team to investigate and address as necessary, in a future maintenance release.

 

Link to comment
Share on other sites

@Clover13 To reproduce the bug, the database must have the Store revisions option enabled. Most likely the test was made on a database with it disabled.

I initially thought too the error was coming from saving the record to the database, only after re-checking your last screenshot I noticed it was a revision instead. 😅

Link to comment
Share on other sites

6 minutes ago, teraßyte said:

@Clover13 To reproduce the bug, the database must have the Store revisions option enabled. Most likely the test was made on a database with it disabled.

I initially thought too the error was coming from saving the record to the database, only after re-checking your last screenshot I noticed it was a revision instead. 😅

I see how you tracked it down in the PHP code, I'll try to do that next time as well.  I'm not too PHP savvy, but I see the stacktrace there and how you tracked it down, thank you for doing that! 👍

Also confirming with revisions OFF for the Pages DB, it does work and returns a 201 (CREATED)

2024-03-21 11:12:21 - INFO - post_cms_record_url: http://localhost/api/index.php?/cms/records/1
2024-03-21 11:12:21 - INFO - post_data: {'category': 1, 'author': 1, 'fields[1]': 'Title 123', 'fields[2]': 'Content ABC'}
2024-03-21 11:12:21 - DEBUG - Starting new HTTP connection (1): localhost:80
2024-03-21 11:12:21 - DEBUG - http://localhost:80 "POST /api/index.php?/cms/records/1&key=ef2f4f1687b901d3c962f074dcd4528e HTTP/1.1" 201 1109

 

Link to comment
Share on other sites

  • 1 month later...
  • Recently Browsing   0 members

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