---
title: Web.Contents – ดึงข้อมูลจากเว็บและ REST API
url: https://www.thepexcel.com/functions/power-query/accessing-data-functions/web-contents/
type: function-explainer
program: Power Query
syntax: "Web.Contents(url as text, optional options as nullable record) as binary"
date: 2025-12-19
scores:
  popularity: 8
  difficulty: 5
  usefulness: 9
---

# Web.Contents – ดึงข้อมูลจากเว็บและ REST API

> ดาวน์โหลดข้อมูลจาก URL เป็น binary พร้อมรองรับ REST API authentication และ dynamic URL construction

## คำอธิบาย

Web.Contents เป็นฟังก์ชันสำหรับดาวน์โหลดเนื้อหาจากเว็บไซต์หรือแหล่งข้อมูลบนอินเทอร์เน็ตผ่าน URL และคืนค่าเป็นข้อมูลแบบ binary data ที่สามารถนำไปประมวลผลต่อได้
.
รองรับการเชื่อมต่อและเรียกใช้งาน REST API อย่างครบถ้วนพร้อมระบบการยืนยันตัวตนผ่านทางตัวเลือกต่างๆ เช่น การกำหนดค่า Headers สำหรับส่งข้อมูลเพิ่มเติมในคำขอ การใช้ Query parameters สำหรับการกรองข้อมูลและส่งพารามิเตอร์ และการระบุ ApiKeyName เพื่อจัดเก็บคีย์การเข้าถึงอย่างปลอดภัย
.
เหมาะอย่างยิ่งสำหรับการเชื่อมต่อกับบริการเว็บและจุดเชื่อมต่อ API แบบไดนามิก โดยใช้ความสามารถของ RelativePath และตัวเลือก Query เพื่อสร้าง URL แบบยืดหยุ่นและทำให้กระบวนการรีเฟรชข้อมูลทำงานได้อย่างราบรื่นและมีประสิทธิภาพในระบบ Power BI Service

## Syntax

```excel
Web.Contents(url as text, optional options as nullable record) as binary
```

**Variant**

```excel
Web.Contents(url)
```

รูปแบบพื้นฐาน - ดาวน์โหลดข้อมูลจาก URL โดยไม่มี options

**Variant**

```excel
Web.Contents(url, [Query = ...])
```

ใช้ Query parameters แบบ dynamic สำหรับ GET request

**Variant**

```excel
Web.Contents(url, [Headers = ..., Content = ...])
```

ส่ง POST request พร้อม custom headers และ request body

**Variant**

```excel
Web.Contents(url, [RelativePath = ..., Query = ...])
```

สร้าง dynamic URL ด้วย RelativePath และ Query parameters

**Variant**

```excel
Web.Contents(url, [ApiKeyName = "key"])
```

ใช้ API key authentication แบบปลอดภัย

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| url | Yes | text |  | ที่อยู่ URL ของแหล่งข้อมูลบนเว็บที่ต้องการดาวน์โหลดและดึงข้อมูลเข้ามา สามารถระบุเป็น HTTP protocol หรือ HTTPS protocol ก็ได้ โดยระบบจะทำการ upgrade จาก HTTP ไปเป็น HTTPS ให้โดยอัตโนมัติเพื่อความปลอดภัย สำหรับกรณีที่ต้องการสร้าง dynamic URLs ที่เปลี่ยนแปลงได้ตามสถานการณ์ ควรระบุเฉพาะ base URL ที่คงที่ไว้ในพารามิเตอร์นี้ แล้วใช้พารามิเตอร์ RelativePath สำหรับระบุส่วนของ path ที่เปลี่ยนแปลงแบบไดนามิก |
| options | No | nullable record | null | Record ที่มี optional properties สำหรับควบคุม HTTP request behavior รวมถึง Query, Headers, Timeout, RelativePath, ApiKeyName, Content, ExcludedFromCacheKey, IsRetry, และ ManualStatusHandling |
| Query (in options) | No | record | null | Record ของ query parameters ที่จะเพิ่มใน URL โดย Power Query จะทำ URL encoding ให้อัตโนมัติ เหมาะสำหรับสร้าง dynamic API calls โดยไม่ต้องทำ string concatenation ด้วยตัวเอง |
| ApiKeyName (in options) | No | text | null | ชื่อ (ไม่ใช่ค่า) ของ API key parameter สำหรับ authentication ค่าจริงจะถูกดึงจาก Power Query credentials (Web API authentication) และแทรกเข้าไปใน request อัตโนมัติ เพื่อความปลอดภัยและป้องกันการเปิดเผย credentials ใน M code |
| Headers (in options) | No | record | null | Record ของ HTTP headers ที่ต้องการส่งไปพร้อม request เช่น Content-Type, Authorization (Bearer token), Accept, User-Agent เป็นต้น field names ที่มี special characters ต้องใช้ #"Header-Name" syntax |
| Timeout (in options) | No | duration | #duration(0, 0, 1, 40) | ระยะเวลา timeout สำหรับ HTTP request ใช้ #duration() function เพื่อระบุค่า เช่น #duration(0, 0, 5, 0) สำหรับ 5 นาที ค่า default คือ 100 วินาที เหมาะสำหรับ API endpoints ที่ response ช้า |
| ExcludedFromCacheKey (in options) | No | list | null | List ของ HTTP header keys ที่ไม่ต้องการนำมาคำนวณใน cache key เหมาะสำหรับ headers ที่เปลี่ยนแปลงบ่อยแต่ไม่ส่งผลต่อ response เช่น request timestamp หรือ correlation IDs |
| IsRetry (in options) | No | logical | false | ตั้งค่าเป็น true เพื่อบังคับให้ดึงข้อมูลใหม่และไม่ใช้ cached response มีประโยชน์เมื่อต้องการ fresh data หรือเมื่อทดสอบ API changes |
| ManualStatusHandling (in options) | No | list | null | List ของ HTTP status codes ที่ต้องการจัดการเอง แทนที่จะใช้ built-in error handling ของ Power Query เช่น {404, 500} เหมาะสำหรับ custom error handling logic |
| RelativePath (in options) | No | text | null | Path ที่จะเพิ่มต่อท้าย base URL ก่อนทำ request ใช้สำหรับสร้าง dynamic endpoints โดยที่ Power Query ยังคงรู้จัก base URL เป็น data source เดียวกัน ซึ่งช่วยให้ refresh ใน Power BI Service ทำงานได้ถูกต้อง |
| Content (in options) | No | binary | null | Request body สำหรับ POST request เมื่อระบุ Content จะเปลี่ยน HTTP method จาก GET เป็น POST อัตโนมัติ มักใช้ร่วมกับ Json.FromValue() เพื่อแปลง record เป็น JSON binary สำหรับส่งไปยัง REST APIs |

## เคสการใช้งาน

### ดาวน์โหลดไฟล์จาก URL

ใช้ Web.Contents ดาวน์โหลดไฟล์ CSV, JSON, XML หรือ Excel จาก web URL แล้วนำมาประมวลผลด้วย Table functions, Json.Document หรือ Xml.Document

_เหมาะกับ:_ data-import

### เชื่อมต่อกับ REST API แบบ GET

ดึงข้อมูลจาก REST API endpoints โดยใช้ Query parameters สำหรับ filtering และ pagination ร่วมกับ Json.Document เพื่อแปลง JSON response เป็น table structure

_เหมาะกับ:_ api-integration

### ส่งข้อมูลไปยัง API ด้วย POST request

ใช้ Content parameter ส่ง JSON payload ไปยัง API endpoint เพื่อ create, update หรือ query ข้อมูลแบบ complex โดยใช้ Headers สำหรับ authentication และ content type

_เหมาะกับ:_ api-integration

### API Authentication ด้วย API Keys

ใช้ ApiKeyName parameter สำหรับ authenticate กับ APIs ที่ต้องการ API key โดยเก็บ key ใน Power Query credentials แทนการฝังใน M code เพื่อความปลอดภัยและให้ refresh ทำงานได้ใน Power BI Service

_เหมาะกับ:_ secure-connection

### Dynamic API Calls ด้วย RelativePath

สร้าง dynamic API endpoints โดยใช้ RelativePath และ Query parameters เพื่อให้ Power Query รู้จัก base URL และทำ refresh ได้อัตโนมัติ เหมาะสำหรับ APIs ที่มี resource IDs หรือ date ranges ที่เปลี่ยนแปลง

_เหมาะกับ:_ dynamic-queries

## ตัวอย่าง

### 1. ตัวอย่างที่ 1: ดาวน์โหลดไฟล์ JSON จาก URL แบบพื้นฐาน

```excel
let
    Source = Web.Contents("https://api.example.com/data.json"),
    JsonData = Json.Document(Source)
in
    JsonData
```

**ผลลัพธ์:** `Record หรือ List ขึ้นกับโครงสร้าง JSON`

ดาวน์โหลด JSON file จาก URL และแปลงเป็น M record/list ด้วย Json.Document
.
นี่คือรูปแบบพื้นฐานที่สุดของการใช้ Web.Contents สำหรับดึงข้อมูล JSON โดยไม่มี authentication หรือ parameters พิเศษ

### 2. ตัวอย่างที่ 2: เรียก API ด้วย Query Parameters แบบ Dynamic

```excel
let
    BaseUrl = "https://api.example.com/products",
    Category = "electronics",
    MinPrice = 100,
    Response = Web.Contents(
        BaseUrl,
        [
            Query = [
                category = Category,
                minPrice = Number.ToText(MinPrice),
                limit = "50"
            ]
        ]
    ),
    JsonResponse = Json.Document(Response)
in
    JsonResponse
```

**ผลลัพธ์:** `JSON response จาก API พร้อม query string: ?category=electronics&minPrice=100&limit=50`

ใช้ Query parameter ใน options record เพื่อสร้าง URL query string แบบ dynamic โดยไม่ต้องทำ text concatenation ด้วยตัวเอง
.
Power Query จะทำ URL encoding ให้อัตโนมัติ และรู้จักว่า base URL เป็น data source เดียวกัน ซึ่งช่วยให้ refresh ทำงานได้ดีใน Power BI Service 💡

### 3. ตัวอย่างที่ 3: Authentication ด้วย Bearer Token ใน Headers

```excel
let
    ApiUrl = "https://api.example.com/secure/data",
    Response = Web.Contents(
        ApiUrl,
        [
            Headers = [
                #"Content-Type" = "application/json",
                #"Authorization" = "Bearer YOUR_TOKEN_HERE",
                #"Accept" = "application/json"
            ]
        ]
    ),
    JsonData = Json.Document(Response),
    DataTable = Table.FromRecords(JsonData[results])
in
    DataTable
```

**ผลลัพธ์:** `Table จาก authenticated API response`

ส่ง HTTP headers พร้อม request เพื่อ authenticate กับ API โดยใช้ Bearer token ใน Authorization header และระบุ Content-Type และ Accept headers
.
หมายเหตุ: ใน production ควรใช้ ApiKeyName หรือเก็บ token ใน credentials แทนการฝังใน M code โดยตรงนะครับ

### 4. ตัวอย่างที่ 4: ใช้ RelativePath สำหรับ Dynamic Endpoints

```excel
let
    BaseUrl = "https://api.example.com/v1",
    ResourceType = "customers",
    ResourceId = "12345",
    Response = Web.Contents(
        BaseUrl,
        [
            RelativePath = ResourceType & "/" & ResourceId,
            Query = [
                include = "orders,profile",
                format = "json"
            ]
        ]
    ),
    JsonData = Json.Document(Response)
in
    JsonData
```

**ผลลัพธ์:** `Customer data จาก URL: https://api.example.com/v1/customers/12345?include=orders,profile&format=json`

ใช้ RelativePath เพื่อสร้าง dynamic endpoint path ที่เปลี่ยนแปลงตาม ResourceType และ ResourceId โดยที่ Power Query ยังคงรู้จัก BaseUrl เป็น data source เดียวกัน
.
วิธีนี้ดีกว่าการทำ text concatenation เพราะช่วยให้ refresh behavior ทำงานได้ถูกต้องใน scheduled refresh และ Power BI Service ครับ 😎

### 5. ตัวอย่างที่ 5: ส่ง POST Request ด้วย JSON Payload

```excel
let
    ApiUrl = "https://api.example.com/search",
    SearchCriteria = [
        keyword = "Power Query",
        category = "tutorials",
        limit = 20
    ],
    JsonPayload = Json.FromValue(SearchCriteria),
    Response = Web.Contents(
        ApiUrl,
        [
            Headers = [
                #"Content-Type" = "application/json",
                #"Accept" = "application/json"
            ],
            Content = JsonPayload
        ]
    ),
    JsonResponse = Json.Document(Response),
    ResultsTable = Table.FromList(
        JsonResponse[results],
        Splitter.SplitByNothing(),
        null,
        null,
        ExtraValues.Error
    )
in
    ResultsTable
```

**ผลลัพธ์:** `Table จาก POST API response พร้อม search results`

ส่ง POST request โดยใช้ Content parameter ที่มี JSON payload ที่สร้างจาก record ด้วย Json.FromValue
.
เมื่อระบุ Content จะเปลี่ยน HTTP method จาก GET เป็น POST อัตโนมัติ เหมาะสำหรับ APIs ที่ต้องการ complex query criteria หรือต้องการส่งข้อมูลจำนวนมากผ่าน request body แทนการใช้ query string

### 6. ตัวอย่างที่ 6: API Key Authentication แบบปลอดภัยด้วย ApiKeyName

```excel
let
    ApiUrl = "https://api.example.com/data",
    Response = Web.Contents(
        ApiUrl,
        [
            RelativePath = "customers/search",
            Query = [
                country = "TH",
                status = "active"
            ],
            ApiKeyName = "api_key"
        ]
    ),
    JsonData = Json.Document(Response),
    CustomersTable = Table.FromRecords(JsonData[customers])
in
    CustomersTable
```

**ผลลัพธ์:** `Table ของ customer data พร้อม URL: https://api.example.com/data/customers/search?country=TH&status=active&api_key=******`

ใช้ ApiKeyName parameter เพื่อระบุชื่อของ API key parameter (เช่น "api_key", "apikey", "token") โดยไม่ต้องใส่ค่าจริงใน M code
.
ค่า API key จริงจะถูกดึงจาก Power Query credentials (Web API authentication) และแทรกเข้าไปใน URL อัตโนมัติ วิธีนี้ปลอดภัยกว่าการฝัง API key ใน M code และทำให้ refresh ทำงานได้ใน Power BI Service ครับ

### 7. ตัวอย่างที่ 7: จัดการ Timeout และ Retry สำหรับ API ที่ช้า

```excel
let
    ApiUrl = "https://api.example.com/large-dataset",
    Response = Web.Contents(
        ApiUrl,
        [
            Query = [limit = "10000"],
            Timeout = #duration(0, 0, 10, 0),
            IsRetry = true,
            Headers = [
                #"Accept-Encoding" = "gzip",
                #"User-Agent" = "PowerQuery/1.0"
            ]
        ]
    ),
    JsonData = Json.Document(Response),
    BufferedData = Binary.Buffer(Response)
in
    JsonData
```

**ผลลัพธ์:** `JSON data พร้อมการจัดการ timeout 10 นาที และ bypass cache`

ตั้งค่า Timeout เป็น 10 นาที (แทน default 100 วินาที) สำหรับ API endpoints ที่ response ช้า
.
ใช้ IsRetry = true เพื่อบังคับให้ดึงข้อมูลใหม่และไม่ใช้ cached response เหมาะสำหรับ testing หรือเมื่อต้องการ fresh data ทุกครั้ง และใช้ Binary.Buffer เพื่อ cache ผลลัพธ์ใน memory หลังจากดาวน์โหลดแล้ว

## หมายเหตุเพิ่มเติม

⚠️ สำคัญ: เมื่อใช้ Web.Contents ใน production ควรใช้ RelativePath และ Query parameters แทน text concatenation เพื่อให้ refresh ทำงานได้ดีใน Power BI Service
.
ที่ต้องระวังคือควรใช้ ApiKeyName สำหรับ secure API authentication แทนการฝัง credentials ใน M code โดยตรง นอกจากนี้ควรตั้งค่า Timeout ที่เหมาะสมสำหรับ API endpoints ที่ response ช้า และใช้ Binary.Buffer เพื่อ cache responses ที่ใช้ซ้ำหลายครั้งนะครับ 💡

## คำถามที่พบบ่อย

**Q: Web.Contents ต่างจาก Web.Page อย่างไร?**

Web.Contents คืนค่า binary data จาก URL เหมาะสำหรับดึงไฟล์ JSON, CSV, XML หรือเรียก REST APIs ส่วน Web.Page ใช้สำหรับ parse HTML content และแปลงเป็น tables จาก HTML tables ใน webpage
.
ถ้าต้องการดึงข้อมูลจาก API ใช้ Web.Contents ร่วมกับ Json.Document แต่ถ้าต้องการ scrape ข้อมูลจาก HTML tables ใช้ Web.Page ครับ

**Q: ทำไม API refresh ใน Power BI Service ล้มเหลวบ่อย?**

สาเหตุหลักคือ Power BI Service มองว่า URL ที่ต่างกันเป็นคนละ data source เมื่อใช้ text concatenation สร้าง dynamic URLs
.
แนะนำให้ใช้ RelativePath และ Query parameters แทนการทำ & "?param=" & value เพื่อให้ Power Query รู้จัก base URL เป็น data source เดียวกัน นอกจากนี้ต้องแน่ใจว่า credentials ถูกตั้งค่าใน Power BI Service สำหรับ base URL นั้นนะครับ

**Q: จะเก็บ API Key อย่างไรให้ปลอดภัยใน Power Query?**

ใช้ ApiKeyName parameter แทนการฝัง API key ใน M code โดยระบุเฉพาะชื่อ parameter (เช่น "api_key") แล้วตั้งค่า Web API credentials ใน Power Query โดยใส่ URL และ API key value
.
ค่าจริงจะถูกแทรกเข้าไปใน request อัตโนมัติโดยไม่แสดงใน M formula วิธีนี้ปลอดภัยและทำให้ publish ไปยัง Power BI Service ได้โดยไม่เปิดเผย credentials ครับ 😎

**Q: Web.Contents รองรับ POST requests ได้อย่างไร?**

ระบุ Content parameter ใน options record จะเปลี่ยน HTTP method จาก GET เป็น POST อัตโนมัติ
.
โดยทั่วไปจะใช้ Json.FromValue() แปลง M record เป็น JSON binary แล้วส่งเป็น Content พร้อมตั้งค่า Headers = [#"Content-Type" = "application/json"] สำหรับบอก server ว่าเป็น JSON payload หมายเหตุ: POST requests ใน Power Query จำเป็นต้องใช้ anonymous authentication ครับ

**Q: Query Folding ทำงานกับ Web.Contents หรือไม่?**

Web.Contents เองไม่รองรับ query folding เพราะเป็นการดาวน์โหลดข้อมูลจาก web endpoint ที่ไม่มี query engine
.
แต่ถ้า API รองรับ server-side filtering/pagination คุณสามารถใช้ Query parameters หรือ RelativePath เพื่อส่ง filters ไปให้ API ทำงานฝั่ง server ซึ่งให้ผลลัพธ์คล้าย query folding โดยลด data transfer และเพิ่มความเร็วครับ

**Q: ทำไมต้องใช้ RelativePath แทน Text Concatenation?**

RelativePath ช่วยให้ Power Query รู้จัก base URL และ dynamic parts แยกจากกัน
.
เมื่อใช้ text concatenation เช่น BaseUrl & "/customers/" & ID จะทำให้แต่ละ URL ถูกมองเป็นคนละ data source ทำให้ refresh ใน Power BI Service ล้มเหลวหรือต้องตั้งค่า credentials หลายครั้ง ส่วน RelativePath จะทำให้ Power Query รู้ว่าทุก endpoint ใช้ credentials เดียวกันจาก base URL ครับ

**Q: จะจัดการ HTTP error codes (404, 500) อย่างไร?**

ใช้ ManualStatusHandling parameter เพื่อระบุ HTTP status codes ที่ต้องการจัดการเอง เช่น [ManualStatusHandling = {404, 500}] จะทำให้ Power Query ไม่ throw error เมื่อเจอ status code เหล่านี้
.
แล้วใช้ try...otherwise เพื่อ handle errors และคืนค่า default หรือใช้ Web.Contents(...)[Response][StatusCode] เพื่อตรวจสอบ status code แล้วทำ conditional logic ตามต้องการครับ

**Q: จะใช้ Binary.Buffer กับ Web.Contents ยังไง?**

Binary.Buffer(Web.Contents(url)) จะ cache binary response ใน memory หลังจากดาวน์โหลดครั้งแรก
.
ถ้ามีการอ้างอิง result หลายครั้งใน query จะไม่ต้องดาวน์โหลดซ้ำ เหมาะสำหรับ API responses ขนาดใหญ่ที่ใช้ในหลาย steps หรือเมื่อต้องการลด API calls แต่ระวังการใช้ memory มากเกินไปถ้าไฟล์ใหญ่มากนะครับ 😅

## ฟังก์ชันที่เกี่ยวข้อง

- [Json.Document – แปลง JSON Text เป็น Power Query Record](https://www.thepexcel.com/functions/power-query/accessing-data-functions/json-document/)
- [Json.FromValue – แปลง Value เป็น JSON](https://www.thepexcel.com/functions/power-query/text-functions/json-fromvalue/)
- [Web.Page](https://www.thepexcel.com/functions/power-query/accessing-data-functions/web-page/)
- [Binary.Buffer – สร้างค่า Binary ที่เสถียร](https://www.thepexcel.com/functions/power-query/binary-functions/binary-buffer/)
- [Table.FromRecords – สร้างตารางจากรายการ Record](https://www.thepexcel.com/functions/power-query/table-functions/table-fromrecords/)
- [Table.FromList – สร้างตารางจาก List](https://www.thepexcel.com/functions/power-query/table-functions/table-fromlist/)
- [Table.FromColumns – สร้างตารางจากลิสต์ของคอลัมน์](https://www.thepexcel.com/functions/power-query/table-functions/table-fromcolumns/)
- [Number.ToText – แปลงตัวเลขเป็นข้อความ](https://www.thepexcel.com/functions/power-query/number-functions/number-totext/)
- [Splitter.SplitByNothing](https://www.thepexcel.com/functions/power-query/splitter-functions/splitter-splitbynothing/)
- [AccessControlEntry.ConditionToIdentities – แปลงเงื่อนไข ACE](https://www.thepexcel.com/functions/power-query/accessing-data-functions/accesscontrolentry-conditiontoidentities/)
- [AdoDotNet.DataSource – เชื่อมต่อ ADO.NET](https://www.thepexcel.com/functions/power-query/accessing-data-functions/adodotnet-datasource/)

## แหล่งข้อมูลเพิ่มเติม

- [Microsoft Learn: Web.Contents](https://learn.microsoft.com/en-us/powerquery-m/web-contents) _(documentation)_
- [Microsoft Learn: Handling HTTP Status Codes](https://learn.microsoft.com/en-us/power-query/handling-status-codes) _(documentation)_
- [Microsoft Learn: Accessing Web Data in Power Query](https://learn.microsoft.com/en-us/power-query/connectors/web/web) _(documentation)_
- [PowerQuery.how: Web.Contents Best Practices](https://powerquery.how/web-contents/) _(guide)_

---

_Source: [https://www.thepexcel.com/functions/power-query/accessing-data-functions/web-contents/](https://www.thepexcel.com/functions/power-query/accessing-data-functions/web-contents/)_
