ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

How to use HTTP_API to redirect browser to new URL. Is it possible?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use HTTP_API to redirect browser to new URL. Is it possible?


    Hello,

    Our member website is written using RPGLE and HTTPAPI on the backend. I am attempting to assist a co-worker whose been assigned a new task and we've gotten a bit stuck. I thought posting a question here might help out. The question, is how to send the user to new URL using backend code. Maybe the question is...is that possible?

    In a nutshell, she has been tasked with writing a new web page that sends billing information to a 3rd party which would handle the payment. She uses http_post to pass on all the data needed including a unique ID, and then receives a response from the 3rd party domain. If successful, we need to pass on (redirect?) the browser to that page so that the user can enter their payment information securely on the 3rd party domain, before that user is passed back to us.

    We are getting the response we expect from the 3rd party and it is successful, but now we need to tell the browser to go to the 3rd party URL. We know the URL, but we can't seem to figure out how to do that programatically. How is that done? Or is the only answer to build the html with the 3rd party URL on our page (display a button on our page if you will with an HREF pointing to that URL) and have the user click that link.

    Thanks for any help. We appreciate your time!
    Last edited by KMPrenger; February 12, 2020, 12:58 PM.

  • #2
    HTTPAPI is not used to build your web page or to communicate with the browser. Probably, you're using it to communicate for the billing info to the 3rd party.

    In order to tell you how to redirect your web page / browser, we'd need to know how you are developing this web page. For example, are you using CGI from RPG? If so, which toolset for CGI (CGIDEV2, eRPGSDK, RPGsp, RSP, etc)? Or, are you interfacing with a different technology to provide the web page, and if so, what?

    It is the code that interfaces with the web page that needs to be modified to make the browser redirect, not HTTPAPI -- so if we don't know what technology you're using, we can't help you.

    Comment


    • #3
      I'm sorry for that confusion. The page is using CGIDEV2, I should have mentioned that earlier. But I will admit that we felt there was a way to do this using HTTPAPI functions as well, which is clearly a miss-understanding.
      Last edited by KMPrenger; February 12, 2020, 01:00 PM.

      Comment


      • #4
        So you want to receive a request via CGIDEV2 and have the response redirect the user's browser to a different page? I'd suggest Javascript. Here's a snippet that will open a URL in a different browser window. I'm sure there's a Javascript function to navigate the current window to a different URL.

        Code:
        <!DOCTYPE html>
        <html lang="en">
        <head>
        <script>
            window.open(parameters_go_here);
        </script>
        </head>
        <body>
        </body>
        </html>

        Comment


        • #5
          Originally posted by jtaylor___ View Post
          So you want to receive a request via CGIDEV2 and have the response redirect the user's browser to a different page? I'd suggest Javascript. Here's a snippet that will open a URL in a different browser window. I'm sure there's a Javascript function to navigate the current window to a different URL.

          Code:
          <!DOCTYPE html>
          <html lang="en">
          <head>
          <script>
          window.open(parameters_go_here);
          </script>
          </head>
          <body>
          </body>
          </html>
          Hello jtaylor,

          Something like that could work. Example: "window.location.href ="http://[url here]";"

          If we get the successful response from the 3rd party, we could write the JavaScript to the page and execute it once the page reloads, causing the automatic redirect.

          I guess we were hoping to have it all happen in the background programmatically, where after the successful response, it would execute similar behavior as just clicking on a link. We may need to seek out other solutions such as yours. Still digging.

          Comment


          • #6
            In CGIDEV2 you create a template that you can use to output a web page. Different companies use very different sstyles for their templates, which makes it tricky for me to give you an example, but... In a typical page you'd have something like this:
            Code:
            /$Heading
            Content-Type: text/html
            
            <html><body>Some data here: /%myvar%/</body></html>
            Whether you use /$ for the template section names and /% %/ for the template variable names is what varies the most. These are teh defaults, but many override them to something else. Assuming you are using the defaults, an easy way to do a redirect would be to do this:

            Code:
            /$Redirect
            Status: 302 Found
            Location: /%newurl%/
            In CGI, everything up to the first blank line is an HTTP keyword (rather than actual data). That's why you normally start with "content-type" which is the HTTP keyword to indicate the type of data you're sending (text/html is the correct type for an HTML document.)

            There are also keywords to do other things. Status allows you to change the HTTP status code. The default HTTP status is 200, which means "success". You've no doubt seen other statuses while surfing the web, such as 404 Not Found or 403 Forbidden. 302 is a status code that means that the data was found at a different URL. So when I did "Status: 302 Found" above, that tells it to redirect. The "Location:" keyword that follows tells the URL you should redirect to. Then, there must be a blank line beneath it to indicate finishing the keywords -- that's why there's a blank line in my example beneath the Location keyword.

            The /%newurl%/ is the variable from the RPG program that you'd set to the new URL -- this is no different than setting any other variable, so I'm not going to provide the corresponding RPG code, I assume you can figure that out yourself.

            The 302 status was used in the HTTP/1.0 days, so I'm including it in the example in case you work with older browsers that still use this older specification. It should still work with newer browsers, too... If you know you'll only need to support newer browsers, there are separate 303 and 307 status codes that would be preferred. You can learn more here: https://en.wikipedia.org/wiki/HTTP_302

            Comment


            • #7
              looks like the forum software removed my blank line after the 2nd code snippet. There should be a blank line after the "Location" keyword, above.

              Comment


              • #8
                Thanks Scott,

                That sounds like a very good idea also and wouldn't rely on the use of JavaScript. We'll give that a shot.

                In the meantime, I think we may have found another solution for causing an auto redirect: See https://css-tricks.com/snippets/html/meta-refresh/
                <meta http-equiv="refresh" content="5;url=http://example.com/" />
                Using this method, we can control how long until the redirect happens (5 seocnds in the example above), and we can use that time to write out other html to the page to let the user know they are being redirected to an outside domain.
                Last edited by KMPrenger; February 12, 2020, 04:01 PM.

                Comment


                • #9
                  The forum is pulling out my blanks lines before and after the example html element also...

                  Comment


                  • #10
                    Originally posted by KMPrenger View Post
                    ...
                    I guess we were hoping to have it all happen in the background programmatically, where after the successful response, it would execute similar behavior as just clicking on a link. We may need to seek out other solutions such as yours. Still digging.
                    My example would return HTML to the browser that would automatically have the browser execute the Javascript. Are you saying you want CGIDEV2 to return the 3rd party page directly?

                    Comment


                    • #11
                      Originally posted by jtaylor___ View Post
                      My example would return HTML to the browser that would automatically have the browser execute the Javascript. Are you saying you want CGIDEV2 to return the 3rd party page directly?
                      Yes, that is originally what we were trying to do. Not sure if that is possible outside of the example that Scott provided for us above. That said, we are kind of liking the meta tag refresh/redirect action that I posted above also.

                      Appreciate you both for you input on this!

                      Comment


                      • jtaylor___
                        jtaylor___ commented
                        Editing a comment
                        You may run into security issues with that. Browser may balk at your server returning HTML that came from a different server.

                      • KMPrenger
                        KMPrenger commented
                        Editing a comment
                        We didn't intend to return the 3rd party html via our server, but rather, send them (redirect them?) to the 3rd party domain using a CGIDEV2 command from within the RPGLE code. If that makes sense. Sounds like that exact method may not be possible. Instead, it seems like we have to execute a redirect from the browser after returning the page. I thought from the beginning that might be the case, but I just didn't know.

                    • #12
                      Originally posted by KMPrenger View Post
                      Yes, that is originally what we were trying to do. Not sure if that is possible outside of the example that Scott provided for us above. That said, we are kind of liking the meta tag refresh/redirect action that I posted above also.
                      I'm not understanding why the solution I posted doesn't answer your concern? This is the normal web that a web site redirects... Can you explain what is missing from the solution I provided?

                      Comment


                      • #13
                        Originally posted by Scott Klement View Post

                        I'm not understanding why the solution I posted doesn't answer your concern? This is the normal web that a web site redirects... Can you explain what is missing from the solution I provided?
                        Hey Scott,

                        There is nothing wrong with your solution. We tried it out, and it does redirect just like you say. Other's may find this thread and like your solution best for their situation. We weren't aware of a solution like the one you provided before, so we appreciate your insight. But, at this point we are preferring the solution I mentioned above, which is the meta refresh redirect html element. It gives us an intermediate screen that we can let the user know they are being transported to an outside domain before the redirect happens, so there are no surprises. (Yes we could easily say it also on the prior page, but we are liking this right now)

                        All of the solutions provided only work if specific html or script is rendered to the page after the back end RPG code has ran and the page is loaded. Originally, we were wondering if there was an RPGLE / CGIDEV2 command that could be executed from within the program itself to cause the redirect to happen without needing to write anything else out to the page. It seems as though that may not be possible.

                        Anyways, we are satisfied now with the direction we are going. Appreciate your help on the matter!

                        Comment

                        Working...
                        X