jfd/gobble

一个简单的插件,可以直接从模板发送 REST API 请求。

安装: 18

依赖: 0

建议者: 0

安全: 0

星星: 0

关注者: 3

分支: 0

开放问题: 0

类型:craft-plugin

1.2.3 2018-03-19 12:40 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:14:47 UTC


README

一个简单的插件,可以直接从模板发送 REST API 请求。

需求

此插件需要 Craft 3。

安装

要安装插件

  1. 打开终端并进入您的 Craft 项目

    cd /path/to/project

  2. 告诉 Composer 加载插件

     composer require jfd/gobble
    
  3. 在控制面板中,转到设置 → 插件,并为 Gobble 点击“安装”按钮。

使用 Gobble

一个简单示例

{% set response = gobble({
    'url': 'https://example.com/api/endpoint',
    'method': 'GET'
}) %}

<ul>
    {% for item in response.body %}
        <li>{{ item.title }}</li>
    {% endfor %}
</ul>

必需参数

url

要发送请求的完整 URL,包括基本 URL 和端点。

method

用于请求的 HTTP 方法。可以是 GETPOSTPUTPATCHDELETE

可选参数

auth

使用 auth 参数将 HTTP 身份验证参数数组与请求一起传递。数组必须包含索引 [0] 中的用户名、索引 [1] 中的密码,您还可以在索引 [2] 中提供内置的身份验证类型。

{% set response = gobble({
    'url': 'https://example.com/api/endpoint',
    'method': 'GET',
    'auth': [
        'username',
        'password',
        'digest' // optional
    ]
}) %}

body

使用 body 参数将内容与实体封装请求(例如 PUT、POST、PATCH)一起传递。

{% set response = gobble({
    'url': 'https://example.com/api/endpoint',
    'method': 'POST',
    'body': 'Some body content...'
}) %}

headers

使用 headers 参数将关联数组形式的头信息与请求一起传递。每个键是头信息的名称,每个值是一个字符串或字符串数组,表示头信息字段值。

{% set response = gobble({
    'url': 'https://example.com/api/endpoint',
    'method': 'GET',
    'headers': {
        'key': 'xxxxxxxxxxxxxxxxx',
        'secret': 'xxxxxxxxxxxxxxxxx'
    }
}) %}

json

使用 json 参数轻松传递作为请求体的 JSON 编码数据。如果请求中已存在 Content-Type 头,将添加 application/json Content-Type 头。

注意: json 参数不能与 body 参数一起使用。如果已定义 json 参数,则忽略 body 参数。

另外:如果已设置 json 参数,将应用具有值 application/jsonContent-Type 头。将忽略模板中设置的任何 Content-Type 头。

{% set response = gobble({
    'url': 'https://example.com/api/endpoint',
    'method': 'GET',
    'json': {
        'key1': 'value1',
        'key2': 'value2'
    }
}) %}

query

使用 query 参数将关联数组形式的查询字符串值或查询字符串与请求一起传递。

注意: query 参数中指定的查询字符串将覆盖请求 URL 中提供的所有查询字符串值。

传递数组

{% set response = gobble({
    'url': 'https://example.com/api/endpoint',
    'method': 'POST',
    'query': {
        'key1': 'value1',
        'key2': 'value2'
    }
}) %}

传递字符串

{% set response = gobble({
    'url': 'https://example.com/api/endpoint',
    'method': 'POST',
    'query': 'key1=value1&key2=value2'
}) %}

响应

响应将以包含四个项的数组形式出现

statusCode

HTTP 状态码

{{ response.statusCode }} // e.g. 200

reasonPhrase

对应的原因短语

{{ response.reasonPhrase }} // e.g. OK

headers

响应头信息。

输出所有

{{ dump(response.headers) }}

/*
array (size=5)
  'Date' => string 'Wed, 17 Jan 2018 17:11:41 GMT' (length=29)
  'Server' => string 'Microsoft-IIS/8.5' (length=17)
  'ContentLength' => string '2668' (length=4)
  'ContentType' => string 'application/json; charset=utf-8' (length=31)
  'XPoweredBy' => string 'ASP.NET' (length=7)
*/

输出单个头信息

{{ response.headers.ContentType }} // application/json; charset=utf-8

body

响应体

示例

{% set response = gobble({
    'url': 'https://example.com/api/endpoint',
    'method': 'GET'
}) %}

{{ response.statusCode }} // 200
{{ response.reasonPhrase }} // OK

<ul>
    {% for item in response.body %}
        <li>{{ item.title }}</li>
    {% endfor %}
</ul>