8 Replies Latest reply: Dec 12, 2017 11:44 AM by Steve Dark RSS

    POST Data using REST Connector

    Kevin Vaccarin

      Hello Community,

       

      I am trying to POST data in from Qlik (sense or view) in BDD using REST connector.

      When I enter the url, the authorisation token , and the format, and press "TEST CONNECTION", I create my lines in the BDD.

      But when I want to "CREATE" the connection I have an error.

       

      I would like to create every new customer.

      Any idea?

        • Re: POST Data using REST Connector
          Steve Dark

          Hi Kevin,

           

          What error is it that you are receiving?

           

          Generally when I pass values into POST fields I put the key value pairs in the boxes further down, rather than putting in the body text.

           

          Using the request body field spaces could well be an issue, I guess.

           

          Have you proven the connection, authorisation etc. using a tool like PostMan?

            • Re: POST Data using REST Connector
              Kevin Vaccarin

              Hi Steve,

               

              Thanks you for your answer,

              I try my request with postman and it work fine.

              my probleme is that I have 20 new client by day and I don't find a way to loop in Postman, so I tried with Qlik.

              My query header are below (the same as Postman):

              And I got this error:

              But the line is created...

                • Re: POST Data using REST Connector
                  Kevin Vaccarin

                  And I also delete the field spaces in my request

                  • Re: POST Data using REST Connector
                    Steve Dark

                    Try removing the Request body and then putting those values in the Query Parameters Name and Value pairs.  When you Test Connection this should then enter the values and prove that approach works.

                     

                    To have it so that you can inject different values each time you will need to clear out those parameters again (that was just to test) and then overwrite them at load time with a WITH CONNECTION statement.  This allows you to overwrite parts of your connection with new values.  The syntax is:

                     

                    FROM JSON (wrap on) "root" PK "__KEY_root"

                    WITH CONNECTION (

                          URL "https://api.twitter.com/oauth2/token",

                          HTTPHEADER "content-type" "application/x-www-form-urlencoded",

                          HTTPHEADER "Authorization" "Basic $(vBASE64Key)",

                          QUERY "grant_type" "client_credentials"

                        );

                     

                    In your case you will have many QUERY rows, but the rest of the connection may be fine stored in the REST connector, so don't need to be included.  Basically the values in the REST connector are either added to (in the case or HTTPHEADER or QUERY rows) or replaced (URL).

                     

                    Hope that helps.


                    Steve

                      • Re: POST Data using REST Connector
                        Kevin Vaccarin

                        Hi Steve,

                         

                        Thank you very much for your answer!

                        I still have an issue, my post request syntax is like below and i have to enter at least 1 "emails" or 1 "phones"; but i don't know how to put the type and value information in the name and value of the "query parameters" of REST conector.

                        ================================================

                        {

                            "first_name": "KevinTest9",

                            "external_id": "8888444466659",

                            "country": "Bosnia and Herzegovina",

                            "phones": [

                                {

                                    "type": "home",

                                    "value": "+41524204209"

                                }

                            ],

                            "address": "710 Vicky Ports",

                            "city": "Port Chandler",

                            "last_name": "Stokes",

                            "emails": [

                                {

                                    "type": "personal",

                                    "value": "kvac9@gmail.com"

                                }

                            ]

                        }

                        ============================================

                        Then about the syntax you gave me:

                         

                        FROM JSON (wrap on) "root" PK "__KEY_root"

                        WITH CONNECTION (

                              URL "https://api.twitter.com/oauth2/token",

                              HTTPHEADER "content-type" "application/x-www-form-urlencoded",

                              HTTPHEADER "Authorization" "Basic $(vBASE64Key)",

                              QUERY "grant_type" "client_credentials"

                            );

                         

                        Once I got the REST connexion made, I only need to replace your values examples by my values, and add query rows, and it will post information?

                         

                        Kevin

                          • Re: POST Data using REST Connector
                            Steve Dark

                            Hi Kevin,

                             

                            Not sure how you would provide a nested value in the Query Headers parameter, or in the QUERY value in the WITH CONNECTION statement.

                             

                            There is also a body tag with the WITH CONNECTION suffix, like this:

                             

                             

                            FROM JSON (wrap on) "root" PK "__KEY_root"

                            WITH CONNECTION (

                                 BODY "$(vRequestBody)"

                                );

                             

                            This will allow you to put the entire request body in a variable and send it in one hit, with the nested part also.

                             

                            The WITH CONNECTION is a good way of getting changing input into your API calls.  We connect to a number of different APIs and use just two connections for all of them, GenericPOST and GenericREST.  Both of these point to a dummy endpoint then whenever we use them the URL and all the parameters are replaced with the values for that particular call.

                             

                            In your original post you said the line is created but an error appears.  It may be that you just need to wrap the call in SET ERRORMODE statements, you will need to check the error state afterwards though, to ensure you don't ignore messages which actually mean the line isn't posted.

                             

                            Hope that gets you a step nearer.

                             

                            Steve

                              • Re: POST Data using REST Connector
                                Sandeep Roy

                                Hi Steve ,

                                 

                                I am facing the problem in passing the request body using WITH Connection statement.

                                 

                                I using simple Google Matrix API to calculate the distance between two places.

                                http://maps.googleapis.com/maps/api/distancematrix/json?origins=Berlin&destinations=hamburg&mode=driving

                                 

                                 

                                I want to pass different origin and destination inn the API as request body.

                                 

                                Kindly suggest..

                                 

                                Response:

                                 

                                {

                                  "destination_addresses" : [ "Hamburg, Deutschland" ],

                                  "origin_addresses" : [ "Berlin, Deutschland" ],

                                  "rows" : [

                                  {

                                  "elements" : [

                                  {

                                  "distance" : {

                                  "text" : "289 km",

                                  "value" : 288832

                                  },

                                  "duration" : {

                                  "text" : "3 Stunden, 0 Minuten",

                                  "value" : 10821

                                  },

                                  "status" : "OK"

                                  }

                                  ]

                                  }

                                  ],

                                  "status" : "OK"

                                }

                                  • Re: POST Data using REST Connector
                                    Steve Dark

                                    Hi Sandeep,

                                     

                                    In this case it should be as simple as not adding any parameters in the URL of the REST connection, or in the Query Parameters section of the connection.  If you test the connection you will get an invalid response returned (as it requires the parameters).

                                     

                                    To add the parameters you would use WITH CONNECTION like this:

                                     

                                    WITH CONNECTION (

                                          QUERY "origins" "$(vOrigins)",

                                          QUERY "destinations" "$(vDestinations)",

                                          QUERY "mode" "$(vMode)",

                                        );

                                     

                                    This way you can set the variables in the script (let vOrigins = 'Berlin';) and then call the same connection for each pair you need a response for.

                                     

                                    Hope that helps?

                                     

                                    Steve