WebAction.Request

D

T

The M Code Behind the Power Query M function WebAction.Request

In this article, we will explore the M code behind the WebAction.Request function and how it can be used to retrieve data from the web.

Understanding WebAction.Request

The WebAction.Request function is used to make HTTP requests to web pages, web services, and APIs. The function takes several parameters, including the URL of the web page or service, the HTTP method to use (GET, POST, PUT, DELETE), and any headers or query parameters to include in the request.

Here is an example of the WebAction.Request function in action:


let

url = "https://www.example.com/api/data",

options = [

Headers = [

#"Content-Type"="application/json"

],

Query = [

limit = 100

]

],

result = WebAction.Request(url, options)

in

result


In this example, we are making a GET request to the URL “https://www.example.com/api/data” with a query parameter of “limit=100”. We are also including a header with the content type of “application/json”. The result of the request is stored in the variable “result”.

Understanding the M Code

Behind the scenes, the WebAction.Request function generates M code that handles the HTTP request and response. The M code is a combination of the URL, method, headers, and query parameters, as well as any authentication or authorization tokens that may be required.

Here is an example of the M code generated by the WebAction.Request function for the previous example:


let

url = "https://www.example.com/api/data",

options = [

Headers = [

#"Content-Type"="application/json"

],

Query = [

limit = 100

]

],

source = Web.Contents(url, options),

json = Json.Document(source)

in

json


In this code, we see that the URL and options are defined as variables. The Web.Contents function is used to make the HTTP request with the specified options. The response from the request is stored in the variable “source”.

The Json.Document function is then used to parse the response into a JSON object. This object can then be further manipulated and transformed using other Power Query functions.

Best Practices for Using WebAction.Request

While the WebAction.Request function is powerful, there are several best practices to keep in mind when using it:

1. Use HTTPS: Always use HTTPS when making requests to web pages or services. This helps to ensure that your requests are secure and cannot be intercepted or modified.

2. Use Authentication: If the web page or service requires authentication, be sure to include the appropriate authentication tokens or headers in your request. This will ensure that you are authorized to make the request and receive the data.

3. Use Query Parameters: When making requests to web services or APIs, always use query parameters to filter or limit the results returned. This will help to ensure that you receive only the data you need and reduce the amount of data transferred.

4. Handle Errors: Be sure to handle errors that may occur when making requests. This includes handling HTTP errors, network errors, and timeouts. Use the try…otherwise…end try construct to handle errors and provide meaningful feedback to the user.

The WebAction.Request function is a powerful tool for retrieving data from web pages, web services, and APIs. By understanding the M code behind the function and following best practices, you can use it to efficiently and securely retrieve the data you need for your analysis and reporting.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)