Forum Discussion

JeffRobson's avatar
JeffRobson
Frequent Visitor
5 years ago
Solved

Power Query REST API with Cookies

I'm trying to extract some data from a REST API service that uses cookies to store the authentication credentials.       let Source = Json.Document(Web.Contents("https://app.siliconexpert.co...
  • JeffRobson's avatar
    5 years ago

    I ended up solving my own problem with a Python workaround.

     

    The API documentation included some Python code which I then modified slightly to produce the results I needed.

     

    I setup a new query using the Python Script connector with the code below as the source:

     

     

     

    from pip._vendor import requests
    import pandas as pd
     
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
     
    s = requests.Session()
     
    # Put your username and password
    username = 'login'
    password = 'password'
    data = 'login=' + username + '&apiKey=' + password
    url = "https://app.siliconexpert.com/ProductAPI/search/authenticateUser"
    r = s.post(url, data=data, headers=headers)
     
    # default is fmt=json
    data = 'fmt=xml&partNumber=[{"partNumber":"bav99wt"}]'
    url = "https://app.siliconexpert.com/ProductAPI/search/listPartSearch"
    r = s.post(url, data=data, headers=headers)
    df = pd.DataFrame(r)
    print(df)

     

     

    This returned a series of rows which I then converted into a single text field using PQ, then parsed into XML & generated the data I needed.

     

    I then edited the code to insert Power Query parameters for the username, password and part number, then converted this to a function and applied it against my data set of part numbers as an invoked function.

     

    Thanks for your help. I hope this helps someone else in future!

    Jeff