Mux Router

通过带所有者范围的 path 路由公开请求。不需要认证。

mux 路由为 /mux/{owner_id}/*。剩余 path 会转发给所有者的 lambda 路由逻辑。

Path 参数

参数 类型 描述
owner_id string 所有者标识符
* path 转发到所有者范围的剩余 path

HTTP 请求

curl -X GET "https://api.hola.cloud/mux/user_123/hello-world"
GET /mux/user_123/hello-world HTTP/1.1
Host: api.hola.cloud
package main

import (
	"fmt"
	"io"
	"net/http"
)

func main() {
	req, err := http.NewRequest("GET", "https://api.hola.cloud/mux/user_123/hello-world", nil)
	if err != nil {
		panic(err)
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	responseBody, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(responseBody))
}
<?php
$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.hola.cloud/mux/user_123/hello-world',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'GET',
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new Exception(curl_error($ch));
}
curl_close($ch);

echo $response;
import requests

response = requests.request(
    "GET",
    "https://api.hola.cloud/mux/user_123/hello-world"
)

print(response.text)
const response = await fetch("https://api.hola.cloud/mux/user_123/hello-world", {
  method: "GET"
});

console.log(await response.text());
const response = await fetch("https://api.hola.cloud/mux/user_123/hello-world", {
  method: "GET"
});

const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.hola.cloud/mux/user_123/hello-world"))
            .method("GET", HttpRequest.BodyPublishers.noBody())
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

示例

curl -X GET "https://api.hola.cloud/mux/user_123/hello-world"
GET /mux/user_123/hello-world HTTP/1.1
Host: api.hola.cloud
package main

import (
	"fmt"
	"io"
	"net/http"
)

func main() {
	req, err := http.NewRequest("GET", "https://api.hola.cloud/mux/user_123/hello-world", nil)
	if err != nil {
		panic(err)
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	responseBody, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(responseBody))
}
<?php
$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.hola.cloud/mux/user_123/hello-world',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'GET',
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new Exception(curl_error($ch));
}
curl_close($ch);

echo $response;
import requests

response = requests.request(
    "GET",
    "https://api.hola.cloud/mux/user_123/hello-world"
)

print(response.text)
const response = await fetch("https://api.hola.cloud/mux/user_123/hello-world", {
  method: "GET"
});

console.log(await response.text());
const response = await fetch("https://api.hola.cloud/mux/user_123/hello-world", {
  method: "GET"
});

const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.hola.cloud/mux/user_123/hello-world"))
            .method("GET", HttpRequest.BodyPublishers.noBody())
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

响应

响应由所有者路由选中的 lambda 产生。

1{
2  "body": {
3    "message": "Hello from mux",
4    "path": "/hello-world"
5  }
6}

错误码

代码 描述
404 所有者或 lambda 路由未找到
500 Lambda 执行错误

评论

发表评论