What is the best way to combine a GET and POST method in a middleware api?












1















I have to create a middleware API which a functionality to check for a key present in my database. If the key exists then it should simply fetch it(GET method). If not, then the API should create the key and its value in the database and return that(POST method). So since we have 2 fundamentally different methods being combined in this API, is it correct to do so? What should be the best way to design such API?










share|improve this question



























    1















    I have to create a middleware API which a functionality to check for a key present in my database. If the key exists then it should simply fetch it(GET method). If not, then the API should create the key and its value in the database and return that(POST method). So since we have 2 fundamentally different methods being combined in this API, is it correct to do so? What should be the best way to design such API?










    share|improve this question

























      1












      1








      1








      I have to create a middleware API which a functionality to check for a key present in my database. If the key exists then it should simply fetch it(GET method). If not, then the API should create the key and its value in the database and return that(POST method). So since we have 2 fundamentally different methods being combined in this API, is it correct to do so? What should be the best way to design such API?










      share|improve this question














      I have to create a middleware API which a functionality to check for a key present in my database. If the key exists then it should simply fetch it(GET method). If not, then the API should create the key and its value in the database and return that(POST method). So since we have 2 fundamentally different methods being combined in this API, is it correct to do so? What should be the best way to design such API?







      node.js rest api http






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 6:40









      Abhishek SinghAbhishek Singh

      61




      61
























          3 Answers
          3






          active

          oldest

          votes


















          2














          Don't combine them.



          Return zero results from your GET method if you the record doesn't exist. Then in the client, if you receive zero results, POST the needed information to another API endpoint.



          Combining the two ideas into one will create a hard to understand system. Your system should be deterministic, i.e. you can always know the result of every call before you call it.






          share|improve this answer































            1














            One way to look at your API is to forget about the underlying database, but think about how an API client uses it.



            If an API client does a GET request, 2 things happen:




            1. The existing record is returned

            2. A new record is created and is returned


            A client might not actually care if 1 or 2 happened. For the perspective of the client, it might look like the resource always existed (even if it was technically just created).



            So as long as there's no extra information that must be sent along with a POST request, it might be fine to use a GET request for both cases.






            share|improve this answer































              0














              I don't know about your situation, typically it is best to have your get and post seperated. Though, if your client thinks that it needs to create a record and then posts the data, i dont see the problem with returning the resource and a 409 for the resource already existing. Here is a similar question HTTP response code for POST when resource already exists



              Then the client can handle the 409 differently or the same as a 200 depending on your needs.






              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465258%2fwhat-is-the-best-way-to-combine-a-get-and-post-method-in-a-middleware-api%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                2














                Don't combine them.



                Return zero results from your GET method if you the record doesn't exist. Then in the client, if you receive zero results, POST the needed information to another API endpoint.



                Combining the two ideas into one will create a hard to understand system. Your system should be deterministic, i.e. you can always know the result of every call before you call it.






                share|improve this answer




























                  2














                  Don't combine them.



                  Return zero results from your GET method if you the record doesn't exist. Then in the client, if you receive zero results, POST the needed information to another API endpoint.



                  Combining the two ideas into one will create a hard to understand system. Your system should be deterministic, i.e. you can always know the result of every call before you call it.






                  share|improve this answer


























                    2












                    2








                    2







                    Don't combine them.



                    Return zero results from your GET method if you the record doesn't exist. Then in the client, if you receive zero results, POST the needed information to another API endpoint.



                    Combining the two ideas into one will create a hard to understand system. Your system should be deterministic, i.e. you can always know the result of every call before you call it.






                    share|improve this answer













                    Don't combine them.



                    Return zero results from your GET method if you the record doesn't exist. Then in the client, if you receive zero results, POST the needed information to another API endpoint.



                    Combining the two ideas into one will create a hard to understand system. Your system should be deterministic, i.e. you can always know the result of every call before you call it.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 25 '18 at 7:09









                    Michael McQuadeMichael McQuade

                    1,1671019




                    1,1671019

























                        1














                        One way to look at your API is to forget about the underlying database, but think about how an API client uses it.



                        If an API client does a GET request, 2 things happen:




                        1. The existing record is returned

                        2. A new record is created and is returned


                        A client might not actually care if 1 or 2 happened. For the perspective of the client, it might look like the resource always existed (even if it was technically just created).



                        So as long as there's no extra information that must be sent along with a POST request, it might be fine to use a GET request for both cases.






                        share|improve this answer




























                          1














                          One way to look at your API is to forget about the underlying database, but think about how an API client uses it.



                          If an API client does a GET request, 2 things happen:




                          1. The existing record is returned

                          2. A new record is created and is returned


                          A client might not actually care if 1 or 2 happened. For the perspective of the client, it might look like the resource always existed (even if it was technically just created).



                          So as long as there's no extra information that must be sent along with a POST request, it might be fine to use a GET request for both cases.






                          share|improve this answer


























                            1












                            1








                            1







                            One way to look at your API is to forget about the underlying database, but think about how an API client uses it.



                            If an API client does a GET request, 2 things happen:




                            1. The existing record is returned

                            2. A new record is created and is returned


                            A client might not actually care if 1 or 2 happened. For the perspective of the client, it might look like the resource always existed (even if it was technically just created).



                            So as long as there's no extra information that must be sent along with a POST request, it might be fine to use a GET request for both cases.






                            share|improve this answer













                            One way to look at your API is to forget about the underlying database, but think about how an API client uses it.



                            If an API client does a GET request, 2 things happen:




                            1. The existing record is returned

                            2. A new record is created and is returned


                            A client might not actually care if 1 or 2 happened. For the perspective of the client, it might look like the resource always existed (even if it was technically just created).



                            So as long as there's no extra information that must be sent along with a POST request, it might be fine to use a GET request for both cases.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 25 '18 at 7:17









                            EvertEvert

                            41.8k1571126




                            41.8k1571126























                                0














                                I don't know about your situation, typically it is best to have your get and post seperated. Though, if your client thinks that it needs to create a record and then posts the data, i dont see the problem with returning the resource and a 409 for the resource already existing. Here is a similar question HTTP response code for POST when resource already exists



                                Then the client can handle the 409 differently or the same as a 200 depending on your needs.






                                share|improve this answer




























                                  0














                                  I don't know about your situation, typically it is best to have your get and post seperated. Though, if your client thinks that it needs to create a record and then posts the data, i dont see the problem with returning the resource and a 409 for the resource already existing. Here is a similar question HTTP response code for POST when resource already exists



                                  Then the client can handle the 409 differently or the same as a 200 depending on your needs.






                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    I don't know about your situation, typically it is best to have your get and post seperated. Though, if your client thinks that it needs to create a record and then posts the data, i dont see the problem with returning the resource and a 409 for the resource already existing. Here is a similar question HTTP response code for POST when resource already exists



                                    Then the client can handle the 409 differently or the same as a 200 depending on your needs.






                                    share|improve this answer













                                    I don't know about your situation, typically it is best to have your get and post seperated. Though, if your client thinks that it needs to create a record and then posts the data, i dont see the problem with returning the resource and a 409 for the resource already existing. Here is a similar question HTTP response code for POST when resource already exists



                                    Then the client can handle the 409 differently or the same as a 200 depending on your needs.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 25 '18 at 13:01









                                    unfloresunflores

                                    971723




                                    971723






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465258%2fwhat-is-the-best-way-to-combine-a-get-and-post-method-in-a-middleware-api%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Costa Masnaga

                                        Fotorealismo

                                        Sidney Franklin