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]?

Comments

  • First, it is my opinion that POST should never identify a new resource in the URL. That is how I read the RFC. If you intend to create a new resource with an identifier in the URL I believe the proper method is PUT.

    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
  • Since there is no separate PUT method in the framework I am trying this using the POST method.

    Yes, I am using PostDatabaseItem method of HTTP_Resource_Services. I'll update the code in HTTP_Resource_Services.

    Thanks.
  • Since there is no separate PUT method in the framework I am trying this using the POST method.
    What do you mean by this? Just add it to your end point API.
  • I think I misunderstood the PostDatabaseItem method, that's why I said that. I guess I use PostDatabaseItem method of HTTP_Resource_Services for PUT as well if I add the method to the endpoint. Am I correct?
  • That's probably what I would do.
  • Make sense. It is working now for both PUT and POST methods. Thanks a lot.
Sign In or Register to comment.