Ruby on Rails basic use of RiotGames API (need explanation, solution already found)












0















First you must know I'm a total beginner, I'm trying to learn so I almost don't know anything.



On the basic page of the API, there is a curl command used as an example to show us how to make requests.



I'm using Ruby on Rails so I used "curl-to-ruby" website to translate it, but it did not work as expected.



I wanted it to show me this :



uri = URI.parse("REQUEST_URL")
response = JSON.parse(Net::HTTP.get(uri))


Instead I got this :



uri = URI.parse("REQUEST_URL")
response = Net:HTTP.get_response(uri)


I don't understand any of this, I thought I wouldn't need to and just use "curl-to-ruby", but apparently I really need to get this.



Would you please try to explain me ?
Or give me links ?
Or matters to read (curl, API, http) ?



Thank you very much, have a nice day.










share|improve this question





























    0















    First you must know I'm a total beginner, I'm trying to learn so I almost don't know anything.



    On the basic page of the API, there is a curl command used as an example to show us how to make requests.



    I'm using Ruby on Rails so I used "curl-to-ruby" website to translate it, but it did not work as expected.



    I wanted it to show me this :



    uri = URI.parse("REQUEST_URL")
    response = JSON.parse(Net::HTTP.get(uri))


    Instead I got this :



    uri = URI.parse("REQUEST_URL")
    response = Net:HTTP.get_response(uri)


    I don't understand any of this, I thought I wouldn't need to and just use "curl-to-ruby", but apparently I really need to get this.



    Would you please try to explain me ?
    Or give me links ?
    Or matters to read (curl, API, http) ?



    Thank you very much, have a nice day.










    share|improve this question



























      0












      0








      0








      First you must know I'm a total beginner, I'm trying to learn so I almost don't know anything.



      On the basic page of the API, there is a curl command used as an example to show us how to make requests.



      I'm using Ruby on Rails so I used "curl-to-ruby" website to translate it, but it did not work as expected.



      I wanted it to show me this :



      uri = URI.parse("REQUEST_URL")
      response = JSON.parse(Net::HTTP.get(uri))


      Instead I got this :



      uri = URI.parse("REQUEST_URL")
      response = Net:HTTP.get_response(uri)


      I don't understand any of this, I thought I wouldn't need to and just use "curl-to-ruby", but apparently I really need to get this.



      Would you please try to explain me ?
      Or give me links ?
      Or matters to read (curl, API, http) ?



      Thank you very much, have a nice day.










      share|improve this question
















      First you must know I'm a total beginner, I'm trying to learn so I almost don't know anything.



      On the basic page of the API, there is a curl command used as an example to show us how to make requests.



      I'm using Ruby on Rails so I used "curl-to-ruby" website to translate it, but it did not work as expected.



      I wanted it to show me this :



      uri = URI.parse("REQUEST_URL")
      response = JSON.parse(Net::HTTP.get(uri))


      Instead I got this :



      uri = URI.parse("REQUEST_URL")
      response = Net:HTTP.get_response(uri)


      I don't understand any of this, I thought I wouldn't need to and just use "curl-to-ruby", but apparently I really need to get this.



      Would you please try to explain me ?
      Or give me links ?
      Or matters to read (curl, API, http) ?



      Thank you very much, have a nice day.







      ruby-on-rails json http parsing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 22:47









      ADyson

      24.4k112545




      24.4k112545










      asked Nov 14 '18 at 22:42









      LoremipsumLoremipsum

      64




      64
























          1 Answer
          1






          active

          oldest

          votes


















          0














          It's because that command doesn't return just the content, it returns the whole HTTP response object including headers and body. You need to extract the response body and parse that using JSON.parse(), e.g.



          JSON.parse(response.body)


          See documentation here: https://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html#method-c-get_response



          (Also, there is nothing in the cURL command which would hint to the converter that the content-type of the response was expected to be JSON (e.g. perhaps an "accepts" header or something), so even if it were able to produce extra code adding the JSON.parse part, it has no way of knowing that it would be appropriate to do so in this case.)






          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%2f53309827%2fruby-on-rails-basic-use-of-riotgames-api-need-explanation-solution-already-fou%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            It's because that command doesn't return just the content, it returns the whole HTTP response object including headers and body. You need to extract the response body and parse that using JSON.parse(), e.g.



            JSON.parse(response.body)


            See documentation here: https://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html#method-c-get_response



            (Also, there is nothing in the cURL command which would hint to the converter that the content-type of the response was expected to be JSON (e.g. perhaps an "accepts" header or something), so even if it were able to produce extra code adding the JSON.parse part, it has no way of knowing that it would be appropriate to do so in this case.)






            share|improve this answer




























              0














              It's because that command doesn't return just the content, it returns the whole HTTP response object including headers and body. You need to extract the response body and parse that using JSON.parse(), e.g.



              JSON.parse(response.body)


              See documentation here: https://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html#method-c-get_response



              (Also, there is nothing in the cURL command which would hint to the converter that the content-type of the response was expected to be JSON (e.g. perhaps an "accepts" header or something), so even if it were able to produce extra code adding the JSON.parse part, it has no way of knowing that it would be appropriate to do so in this case.)






              share|improve this answer


























                0












                0








                0







                It's because that command doesn't return just the content, it returns the whole HTTP response object including headers and body. You need to extract the response body and parse that using JSON.parse(), e.g.



                JSON.parse(response.body)


                See documentation here: https://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html#method-c-get_response



                (Also, there is nothing in the cURL command which would hint to the converter that the content-type of the response was expected to be JSON (e.g. perhaps an "accepts" header or something), so even if it were able to produce extra code adding the JSON.parse part, it has no way of knowing that it would be appropriate to do so in this case.)






                share|improve this answer













                It's because that command doesn't return just the content, it returns the whole HTTP response object including headers and body. You need to extract the response body and parse that using JSON.parse(), e.g.



                JSON.parse(response.body)


                See documentation here: https://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html#method-c-get_response



                (Also, there is nothing in the cURL command which would hint to the converter that the content-type of the response was expected to be JSON (e.g. perhaps an "accepts" header or something), so even if it were able to produce extra code adding the JSON.parse part, it has no way of knowing that it would be appropriate to do so in this case.)







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 22:51









                ADysonADyson

                24.4k112545




                24.4k112545
































                    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%2f53309827%2fruby-on-rails-basic-use-of-riotgames-api-need-explanation-solution-already-fou%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

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Lugert, Oklahoma