简体   繁体   中英

Convert Python Request to Microsoft Power BI M code

I would like to convert a request from Python code to M used in Power BI Power Query

My Python code is

\\

import requests import datetime as dt

headers = {'Authorization': 'Bearer tok_123', 'Accept': 'text/csv'}

url = 'https://api.123/'

params = ( ('start_time', start_date), ('end_time', tomorrow), )

response = requests.get(url, headers=headers, params=params)

data = response.text

\\

The M code I have made (which does not work) is below. Could you let me know where I am going wrong?

Am getting error 400 invalid request. I think there is something wrong with the way I am translating params.

\\

let

apiUrl = "https://api.123/", options = [Headers =[#"Authorization"="Bearer tok_123", #"start_time"="2021-01-05", #"end_time"="2021-02-05"]], result = Web.Contents(apiUrl, options)

in

result

\\

Thanks

I am getting a 400

See the sections below, "verifying request", and "error code details"

Without your url, or the docs, I don't know what endpoint you're using, so here's an example

HTTP GET https://www.example.com/api/user/search?limit=1000&region=US

Using RelativePath and Query

Note: It's important to use options[RelativePath] and options[Query] to prevent service refresh errors on the service. Check out docs: Web.Contents

let 
    Headers = [
        Accept="application/json"
    ],
    BaseUrl = "https://www.example.com",
    Options = [
        RelativePath = "/api/user/search",
        Headers = Headers,
        Query = [
            limit = 1000,
            region = "US"                    
    ],
    Response = Web.Contents(BaseUrl, Options),
    Result = Json.Document(Response) // skip if it's not JSON
in
    Result

Authorization

Setting options[ApiKeyName] lets you specify your API token / password using the credential store instead of in the code itself.

Otherwise you can set it using the Headers record just like you are in python.

Error Code details

For details, first specify the HTTP Status codes you want to handle

Options = [
    // add this param to Options
    ManualStatusHandling = {"400"}
]

Then view error details in the metadata

details = Value.Metadata(Response)

Verifying Request

You can view the actual HTTP requests fired by either using

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM