Problems with POST statements with Plone.RestAPI

This is a Rest.API question. I have a stock Plone 5.2.6 installation. The only eggs are plone.restapi and collective.elasticsearch (the latter is not install, but the former is).

I have some code to create objects. In each instance, Plone returns a status code of 200, not 201 (or anything else). Obviously, the object hasn't been created, but I'm not getting an error message either. The statement:

r = requests.post(url, headers={'Accept': 'application/json', 'Content-Type': 'application/json'}, json={'@type': 'Folder', 'title': title, 'description': description}, auth=(username, secret))

returns r.status_code = 200 (rather than 201). I do the same thing with Postman and get the identical response (200). The thing that I don't understand is why I'm getting a 200 status code rather than something else (like 400 (bad request)) per the RestAPI documentation. The authorization username/password is correct (because I get an "unauthorized" type of response when the password is incorrect. Incidentally, GET statements (just to see if a particular folder is present) works just fine (garnering a 200 status code). Am I missing something?

More information...

I changed from json to data. Essentially:

u = requests.post(http://plone.example.com/folder, data=raw_data, auth=(username, secret))

... where raw_data is a Python dictionary.

The response was even more puzzling. It was 404, not 200. I printed out the URL, and then copied/pasted into a browser and the correct folder came up.

Moreover, a GET statement to the exact same URL returns status code: 200. So I know that the URL is good and the folder object is instantiated.

ronc via Plone Community wrote at 2022-2-9 22:38 +0000:

...
I changed from json to data. Essentially:

u = requests.post(http://plone.example.com/folder, data=raw_data, auth=(username, secret))

... where raw_data is a Python dictionary.

The response was even more puzzling. It was 404, not 200. I printed out the URL, and then copied/pasted into a browser and the correct folder came up.

Reconfigure your error_log object not to ignore "NotFound".
Reproduce and look at the corresponding error_log entry.

I figured out the 404 error. For posterity, I'm going to go over it here to spare some later soul some heartache.

I'm running an internal process. When you set the url to "https://plone.example.com" you get the normal response. However, if you set the url to its IP address "http://10.80.0.45:8080/Plone" you get the 404 error, presumably because of the web server (e.g., Nginx).

Secondly, you get a 200 status code if your requests statement isn't correct. The "trick" is within the requests call statement, namely to have headers set to {"Accept": "application/json", "Content-Type": "application/json"} and not send your python dictionary via "data=" but rather through json=My_Python_Dictionary. Then you'll get the desired 201 response.

1 Like