How to change Sales order status to closed through rest api after submit?

Hi! Through the gui, we can change the ERPNext Sales Order document status to “Closed” after we have submitted it.

There is a button to change the status to closed. We are in a need to do this operation through REST api on some specific Sales Orders. When we try f.e.: requests.put(frappeServerUrl/api/resource/Sales Order/SO, data=json.dumps({"status":"Closed"}), headers=requiredHeaders)

We get a response with status code 417 and a message stating UpdateAfterSubmitError Not allowed to change Status after submission from To Deliver and Bill to Closed even though we can do this from the web ui. What are we doing wrong here?

You can use this function to achieve that.

def close_or_unclose_sales_orders(names, status)

it’s a whitelisted function of Sales Order itself

Thank you for your suggestion. I tried to use that remote method call with the following code:

id = "SAL-ORD-2023-00007"
r ="/api/method/frappe.sales_order.close_or_unclose_sales_orders", 
"names":[id], "status":"Closed"
}), headers=commonHeaders)

This returns 200 status code, but the sales order SAL-ORD-2023-00007 is not marked as closed. What am I doing wrong here?

Do you have an idea why the sales order is still not displayed as closed even when I do the remote function call that returns with 200 status code?