Db2

Db2

Connect with Db2, open source, and other data experts to gain value from your data, share insights, and solve problems.

 View Only
  • 1.  How to use the REST API properly?

    Posted Wed May 20, 2020 04:20 PM
    Hi,

    I am having difficulties in setting up the authentication for Restful api.

    I use the following command to get the token (obviously myuser, mypwd and myserver have concrete values):
    TOKEN=$(curl -s -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' --data '{"userid":"<myuser>","password":"<mypwd>"}' http://<myserver>/dbapi/v4/auth/tokens | jq -r '.id_token')

    and then I try to use it like this:

    curl -X GET http://<myserver>/dbapi/v4/monitor -H 'authorization: Bearer ${TOKEN}' -H 'content-type: application/json'

    But I get the following response:

    {"trace":"","errors":[{"code":"authentication_failure","message":"HWCSEC0003E: Invalid authentication token","target":{"type":"","name":""},"more_info":""}]}

    I can login to the console with the same user and password I used above. What I am doing wrong?

    ------------------------------
    Petri Helin
    ------------------------------

    #Db2


  • 2.  RE: How to use the REST API properly?

    Posted Wed May 20, 2020 04:49 PM
      |   view attached
    Hi Petri,

    You can use the examples in the scripts attached.  It has a practical use case for creating connection profile and granting the connection profile to dmc users.  You can find the description in section 3.3 in the HoL pdf.

    Hope it helps!

    ------------------------------
    Jason Sizto
    ------------------------------

    Attachment(s)

    pdf
    6018.pdf   8.61 MB 1 version


  • 3.  RE: How to use the REST API properly?

    Posted Wed May 20, 2020 05:10 PM
      |   view attached
    Hi Petri,

    Seems like it did not take the scripts with .sh format.  I zipped it up and uploaded again.

    ------------------------------
    Jason Sizto
    ------------------------------

    Attachment(s)

    7z
    Think2020.7z   970 B 1 version


  • 4.  RE: How to use the REST API properly?

    Posted Wed May 20, 2020 06:32 PM
    Edited by System Admin Fri January 20, 2023 04:34 PM
    Thanks Jason. I think I got it with tips from the examples you provided. These at least I got working so far:

    TOKEN=$(curl -s -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' --data '{"userid":"<myuser>","password":"<mypwd>"}' http://<myserver>/dbapi/v4/auth/tokens | jq -r '.id_token')

    curl --insecure -X GET 'http://<myserver>/dbapi/v4/dbprofiles/<mydbprofile>' -H 'authorization: Bearer '$TOKEN -H 'content-type: application/json'

    Response: {"name":"<mydbprofile>","disableDataCollection":"false","databaseVersion":"11.5.0","timeZone":"20000",...

    curl -X GET 'http://<myserver>/dbapi/v4/monitor' -H 'authorization: Bearer '$TOKEN -H 'content-type: application/json' -H 'x-db-profile: <mydbprofile>'

    Response: {"database_service":"online","authentication_service":"online","messages":["Succeed"]}

    ------------------------------
    Petri Helin
    ------------------------------



  • 5.  RE: How to use the REST API properly?

    Posted Mon June 01, 2020 09:17 AM
    You may also want to have a look at my article on DZone: https://dzone.com/articles/db2-dte-pocdb2dmc
    It includes Python as well as script examples using CURL. 

    There is also an extensive Hands on Lab that you can access for free that provides lots of examples and access to sample code to get the most out of the APIs: www.ibm.biz/DMCDemosPOT

    ------------------------------
    Peter Kohlmann
    HDM Digital Technical Engagement
    IBM Canada
    kohlmann@ca.ibm.com
    ------------------------------