Optional parameter in Magento 2 REST APIMagento API - most efficient way to get list of products and their name/url/image_urlMagento 2: Getting a Product URL from the APIMagento 2 API “self” route: pass customer ID as data parameterCustom webapi attribute explanationMagento 2.1.1 Rest API Update price onlyMagento 2.0 REST/SOAP API - how to retrieve Special PriceHow can I create a plugin class for product save method when it is called from API?Magento 2 token integration errorCustom REST API returns duplicate “response” tagI have created A new sample REST Api to GET method, Giving Consumer is not authorized to access the resource error

Boss wants me to falsify a report. How should I document this unethical demand?

Old short story, same personalities, differing planes of existence

What is the most important source of natural gas? coal, oil or other?

What is the object moving across the ceiling in this stock footage?

What is the difference between “/private/var/vm” and “/vm”?

Windows 10 Programs start without visual Interface

Is it possible to play as a necromancer skeleton?

How do I align equations in three columns, justified right, center and left?

Command to Search for Filenames Exceeding 143 Characters?

Employer demanding to see degree after poor code review

Employer asking for online access to bank account - Is this a scam?

Does linking adjectives allow you to talk about multiple variations of something?

Mother abusing my finances

Approximate solution : factorial and exponentials

How to make a crossed out leftrightarrow?

I unknowingly submitted plagiarised work

Placing bypass capacitors after VCC reaches the IC

Binary Search in C++17

Were pens caps holes designed to prevent death by suffocation if swallowed?

What is the difference between nullifying your vote and not going to vote at all?

Array Stutter Implementation

How can people dance around bonfires on Lag Lo'Omer - it's darchei emori?

What are the benefits of cryosleep?

How did early x86 BIOS programmers manage to program full blown TUIs given very few bytes of ROM/EPROM?



Optional parameter in Magento 2 REST API


Magento API - most efficient way to get list of products and their name/url/image_urlMagento 2: Getting a Product URL from the APIMagento 2 API “self” route: pass customer ID as data parameterCustom webapi attribute explanationMagento 2.1.1 Rest API Update price onlyMagento 2.0 REST/SOAP API - how to retrieve Special PriceHow can I create a plugin class for product save method when it is called from API?Magento 2 token integration errorCustom REST API returns duplicate “response” tagI have created A new sample REST Api to GET method, Giving Consumer is not authorized to access the resource error






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I have a API route like,



<route url="/V1/product/:productId" method="GET">
<service class="VendorSampleApiSampleInterface" method="sample"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>


As you can see this route has a required parameter productId. Is it possible to add a optional parameter to this route?



Something like,



<route url="/V1/product/:productId/:optionalParam" method="GET">


Thanks in advance!!










share|improve this question






























    2















    I have a API route like,



    <route url="/V1/product/:productId" method="GET">
    <service class="VendorSampleApiSampleInterface" method="sample"/>
    <resources>
    <resource ref="anonymous"/>
    </resources>
    </route>


    As you can see this route has a required parameter productId. Is it possible to add a optional parameter to this route?



    Something like,



    <route url="/V1/product/:productId/:optionalParam" method="GET">


    Thanks in advance!!










    share|improve this question


























      2












      2








      2


      0






      I have a API route like,



      <route url="/V1/product/:productId" method="GET">
      <service class="VendorSampleApiSampleInterface" method="sample"/>
      <resources>
      <resource ref="anonymous"/>
      </resources>
      </route>


      As you can see this route has a required parameter productId. Is it possible to add a optional parameter to this route?



      Something like,



      <route url="/V1/product/:productId/:optionalParam" method="GET">


      Thanks in advance!!










      share|improve this question
















      I have a API route like,



      <route url="/V1/product/:productId" method="GET">
      <service class="VendorSampleApiSampleInterface" method="sample"/>
      <resources>
      <resource ref="anonymous"/>
      </resources>
      </route>


      As you can see this route has a required parameter productId. Is it possible to add a optional parameter to this route?



      Something like,



      <route url="/V1/product/:productId/:optionalParam" method="GET">


      Thanks in advance!!







      magento2 api






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 15 at 7:06







      Vinoth S

















      asked Feb 14 at 13:32









      Vinoth SVinoth S

      7510




      7510




















          2 Answers
          2






          active

          oldest

          votes


















          1














          It is possible to pass an optional param but not like this <route url="/V1/product/:productId/:optionalParam" method="GET">



          you can pass it with the endpoint by using a question mark(?)



          Suppose your endpoint is this: http://www.example.com/rest/V1/product/3 and if you want to pass some optional parameter, you can pass it like this:



          http://www.example.com/rest/V1/product/3?optional_param=8


          you can retrieve it like this:



          $optional = $this->_request->getParam('optional_param');





          share|improve this answer






























            1














            I may be wrong but I don't think you can declare a specific parameter as optional from within the xml file, though you can declare your routes in such a way that the parameter becomes optional.

            For example if you changed your xml to:



            <route url="/V1/product/:productId" method="GET">
            <service class="VendorSampleApiSampleInterface" method="sample"/>
            <resources>
            <resource ref="anonymous"/>
            </resources>
            </route>
            <route url="/V1/product/:productId/:optionalParam" method="GET">
            <service class="VendorSampleApiSampleInterface" method="sample"/>
            <resources>
            <resource ref="anonymous"/>
            </resources>
            </route>


            So long as the underlying method supports it being optional, something like:



            public function sample($productId, $optionalParam = null)


            This allows the code defined in that method to treat $optionalParam as optional.






            share|improve this answer























              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "479"
              ;
              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: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2fmagento.stackexchange.com%2fquestions%2f261808%2foptional-parameter-in-magento-2-rest-api%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              It is possible to pass an optional param but not like this <route url="/V1/product/:productId/:optionalParam" method="GET">



              you can pass it with the endpoint by using a question mark(?)



              Suppose your endpoint is this: http://www.example.com/rest/V1/product/3 and if you want to pass some optional parameter, you can pass it like this:



              http://www.example.com/rest/V1/product/3?optional_param=8


              you can retrieve it like this:



              $optional = $this->_request->getParam('optional_param');





              share|improve this answer



























                1














                It is possible to pass an optional param but not like this <route url="/V1/product/:productId/:optionalParam" method="GET">



                you can pass it with the endpoint by using a question mark(?)



                Suppose your endpoint is this: http://www.example.com/rest/V1/product/3 and if you want to pass some optional parameter, you can pass it like this:



                http://www.example.com/rest/V1/product/3?optional_param=8


                you can retrieve it like this:



                $optional = $this->_request->getParam('optional_param');





                share|improve this answer

























                  1












                  1








                  1







                  It is possible to pass an optional param but not like this <route url="/V1/product/:productId/:optionalParam" method="GET">



                  you can pass it with the endpoint by using a question mark(?)



                  Suppose your endpoint is this: http://www.example.com/rest/V1/product/3 and if you want to pass some optional parameter, you can pass it like this:



                  http://www.example.com/rest/V1/product/3?optional_param=8


                  you can retrieve it like this:



                  $optional = $this->_request->getParam('optional_param');





                  share|improve this answer













                  It is possible to pass an optional param but not like this <route url="/V1/product/:productId/:optionalParam" method="GET">



                  you can pass it with the endpoint by using a question mark(?)



                  Suppose your endpoint is this: http://www.example.com/rest/V1/product/3 and if you want to pass some optional parameter, you can pass it like this:



                  http://www.example.com/rest/V1/product/3?optional_param=8


                  you can retrieve it like this:



                  $optional = $this->_request->getParam('optional_param');






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 18 at 4:31









                  NIDNID

                  737313




                  737313























                      1














                      I may be wrong but I don't think you can declare a specific parameter as optional from within the xml file, though you can declare your routes in such a way that the parameter becomes optional.

                      For example if you changed your xml to:



                      <route url="/V1/product/:productId" method="GET">
                      <service class="VendorSampleApiSampleInterface" method="sample"/>
                      <resources>
                      <resource ref="anonymous"/>
                      </resources>
                      </route>
                      <route url="/V1/product/:productId/:optionalParam" method="GET">
                      <service class="VendorSampleApiSampleInterface" method="sample"/>
                      <resources>
                      <resource ref="anonymous"/>
                      </resources>
                      </route>


                      So long as the underlying method supports it being optional, something like:



                      public function sample($productId, $optionalParam = null)


                      This allows the code defined in that method to treat $optionalParam as optional.






                      share|improve this answer



























                        1














                        I may be wrong but I don't think you can declare a specific parameter as optional from within the xml file, though you can declare your routes in such a way that the parameter becomes optional.

                        For example if you changed your xml to:



                        <route url="/V1/product/:productId" method="GET">
                        <service class="VendorSampleApiSampleInterface" method="sample"/>
                        <resources>
                        <resource ref="anonymous"/>
                        </resources>
                        </route>
                        <route url="/V1/product/:productId/:optionalParam" method="GET">
                        <service class="VendorSampleApiSampleInterface" method="sample"/>
                        <resources>
                        <resource ref="anonymous"/>
                        </resources>
                        </route>


                        So long as the underlying method supports it being optional, something like:



                        public function sample($productId, $optionalParam = null)


                        This allows the code defined in that method to treat $optionalParam as optional.






                        share|improve this answer

























                          1












                          1








                          1







                          I may be wrong but I don't think you can declare a specific parameter as optional from within the xml file, though you can declare your routes in such a way that the parameter becomes optional.

                          For example if you changed your xml to:



                          <route url="/V1/product/:productId" method="GET">
                          <service class="VendorSampleApiSampleInterface" method="sample"/>
                          <resources>
                          <resource ref="anonymous"/>
                          </resources>
                          </route>
                          <route url="/V1/product/:productId/:optionalParam" method="GET">
                          <service class="VendorSampleApiSampleInterface" method="sample"/>
                          <resources>
                          <resource ref="anonymous"/>
                          </resources>
                          </route>


                          So long as the underlying method supports it being optional, something like:



                          public function sample($productId, $optionalParam = null)


                          This allows the code defined in that method to treat $optionalParam as optional.






                          share|improve this answer













                          I may be wrong but I don't think you can declare a specific parameter as optional from within the xml file, though you can declare your routes in such a way that the parameter becomes optional.

                          For example if you changed your xml to:



                          <route url="/V1/product/:productId" method="GET">
                          <service class="VendorSampleApiSampleInterface" method="sample"/>
                          <resources>
                          <resource ref="anonymous"/>
                          </resources>
                          </route>
                          <route url="/V1/product/:productId/:optionalParam" method="GET">
                          <service class="VendorSampleApiSampleInterface" method="sample"/>
                          <resources>
                          <resource ref="anonymous"/>
                          </resources>
                          </route>


                          So long as the underlying method supports it being optional, something like:



                          public function sample($productId, $optionalParam = null)


                          This allows the code defined in that method to treat $optionalParam as optional.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered May 21 at 12:00









                          AdamAdam

                          314




                          314



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Magento Stack Exchange!


                              • 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%2fmagento.stackexchange.com%2fquestions%2f261808%2foptional-parameter-in-magento-2-rest-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

                              Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

                              Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

                              Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림