Welcome to the SRP Forum! Please refer to the SRP Forum FAQ post if you have any questions regarding how the forum works.
Getting a status "200 OK" instead of "201 Created"
If I send a POST request using a URL like: http://[website.name.com]/api/[endpoint] , I get a status “201 Created” and creates a new row in the database table with a new primary key ID. The value for this new primary key ID is automatically selected by the framework which is the next available row number that was not used before; which is expected to happen.
But if I send a POST request using a URL like: http://[website.name.com]/api/[endpoint]/[an_integer] so that I can use [an_integer] as the new primary key ID, I get a status “200 OK” although it creates a new row in the database table with a new primary key ID that has the value [an_integer]. I was expecting to get a “201 Created” status since it is a newly created row in the database table.
Is there any way I can get “201 Created” status when I send a POST request using a URL http://[website.name.com]/api/[endpoint]/[an_integer]?
But if I send a POST request using a URL like: http://[website.name.com]/api/[endpoint]/[an_integer] so that I can use [an_integer] as the new primary key ID, I get a status “200 OK” although it creates a new row in the database table with a new primary key ID that has the value [an_integer]. I was expecting to get a “201 Created” status since it is a newly created row in the database table.
Is there any way I can get “201 Created” status when I send a POST request using a URL http://[website.name.com]/api/[endpoint]/[an_integer]?
Comments
How are you handling the updating of the database? Are you using the PostDatabaseItem method of HTTP_Resource_Services? If so, then the logic that sets a status code of 200 is near the top as it assumes any Item ID that is pre-populated implies a pre-existing resource rather than a new resource. That code could be improved and updated as follows:
Lock hTable, ItemID then ItemURL = SelfURL Read ItemRec from hTable, ItemID then StatusCode = 200 ; // Updating an existing resource. end else StatusCode = 201 ; // Creating a new resource. ItemRec = '' end HaveItemID = True$ end
Yes, I am using PostDatabaseItem method of HTTP_Resource_Services. I'll update the code in HTTP_Resource_Services.
Thanks.