ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

HTTPAPI and Custom Authorization Header

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

  • HTTPAPI and Custom Authorization Header

    I need to be able to do this:

    Code:
    rc = http_setOption( 'Authorization'                                 
                       : 'myEncodedApiKey123KuanYa#/0*3' );
    The reason I need to be able to do this is because the rest request requires an api key that is not userid/password based. Any thoughts?



  • #2
    So what have you tried so far? It would seem to me you need to use http_setAuth()

    See this thread for a similar situation http://www.scottklement.com/archives.../msg00025.html
    Last edited by JonBoy; May 22, 2017, 02:55 PM.

    Comment


    • #3
      Originally posted by JonBoy View Post
      So what have you tried so far? It would seem to me you need to use http_setAuth()

      See this thread for a similar situation http://www.scottklement.com/archives.../msg00025.html
      http_setAuth() seems to require a userid and password, which I will not have. I tried decoding the 64bit api key i was given, and it looks like non-sense so it's not a userid/password combination.

      Comment


      • #4
        Sorry missed that. So used to seeing requests for user authentication.

        Does the HTTP_POINT_ADDL_HEADER section in this post work for you. Sounds like your situation http://www.scottklement.com/archives.../msg00038.html it is towards th end of the post.

        Comment


        • #5
          Http_setOption() will only set options known to HTTPAPI, so that won't work here. As Jon pointed out, if you want to add your own custom headers, that would be done with the HTTP_POINT_ADDL_HEADER exit procedure. You can use that to put anything you like into the HTTP headers/

          Comment


          • #6
            JonBoy and Scott,

            Thanks for your responses. I'm scrambling trying to prove HTTPAPI is a viable solution for http rest requests directly from the IBM i. The last hurdle is trying to read the response headers. For some reason this 3rd party service returns data in the header response that is needed for other rest requests. /sigh i'll create another thread for that one. I used the example listed in the link above to try to code my own headers. I hit another snag and as soon as I am able to identify it, I will post it.

            Thanks again!

            Comment


            • #7
              Hmm... I didn't see another thread?

              If you need to retrieve a header, there's a procedure called http_header() that should work. I must admit, this one is not used very often... so probably hasn't been tested as heavily as some of the others. But, it's worth looking into:

              You'd run this after the HTTP request is complete:

              Code:
              myString = http_header('the-header-name');
              myString should now contain the contents of the header.

              Comment


              • #8
                Scott,

                Thanks for your response. I did not want to muddy up this post with a problem I am having regarding http_stmf().

                I created a new thread here:

                How do I prevent the Content-Length property from being inserted in the header of an HTTPAPI rest request? For some reason, it is causing the rest api to fail. I can confirm this using a windows tools called Postman. (Scott, I removed my post from the "other" thread).


                Also, the only rest request I have working at this point uses http_setAuth() to add basic authorization to the header. However, when I call another request (which does not need the basic auth header), the header still contains the basic authorization from the previous request. How would I clear the header each time if one job needs to make multiple requests?



                Comment


                • #9
                  My example above will cause your request to fail if you do not put a CRLF at the end of the custom header.

                  Code:
                  d CRLF            c                   const(x'0d25')
                  
                  pHeaders = 'Authorization: ' +                                  
                            'yOur64bitencodedkeyhereenclosedinsinglequotes'+ CRLF;
                  Thanks for your help Scott!
                  Last edited by gcraill; May 30, 2017, 07:41 PM.

                  Comment


                  • TheZenbudda
                    TheZenbudda commented
                    Editing a comment
                    The code was wrong It should be:

                    Code:
                    pHeaders = 'Authorization: ' +                                  
                              'yOur64bitencodedkeyhereenclosedinsinglequotes' + CRLF;
                Working...
                X