Skip to main content

Access Cloud Run via HTTP

Through the HTTP access service, you can configure cloud run services as standard HTTP interfaces, achieving flexible domain and path management. This document explains how to configure HTTP access for cloud run and how to use custom paths.

Prerequisites

  • CloudBase environment created
  • At least one cloud run service deployed

Configure HTTP Access

Step 1: Create Domain Association

  1. Go to CloudBase Console - HTTP Access Service
  2. Click the "New" button in the "Domain Associated Resources" section
  3. Configure the following information:
Configuration ItemDescriptionExample
Associated Resource TypeSelect "Cloud Run", then choose target cloud run servicemy-service
DomainSelect default domain, custom domain, or * (matches all domains)Default domain
Trigger PathSet HTTP access path, supports / or custom path/helloworld

💡 Tip: For production environments, it's recommended to bind a filed custom domain to get full service capabilities. Please refer to Custom Domain Configuration for configuration methods.

Step 2: Test Access

After configuration, access domain + trigger path to invoke the cloud run service:

# Example: Access cloud run service via default domain
curl https://your-env-id.service.tcloudbase.com/helloworld

Make HTTP Requests

After configuration, you can use any HTTP client to access cloud run services.

# GET request
curl https://your-domain/helloworld

# POST request
curl -X POST https://your-domain/helloworld \
-H "Content-Type: application/json" \
-d '{"name": "Zhang San", "email": "zhangsan@example.com"}'

# PUT request
curl -X PUT https://your-domain/helloworld/123 \
-H "Content-Type: application/json" \
-d '{"name": "Li Si"}'

# DELETE request
curl -X DELETE https://your-domain/helloworld/123

Cloud Run Service Development

Receive HTTP Requests

Cloud run services are standard HTTP services and can use any web framework to receive requests:

const express = require('express');
const app = express();

// Parse JSON request body
app.use(express.json());

// GET request
app.get('/helloworld', (req, res) => {
res.json({
users: [
{ id: 1, name: 'Zhang San' },
{ id: 2, name: 'Li Si' }
]
});
});

// POST request
app.post('/helloworld', (req, res) => {
const { name, email } = req.body;

res.json({
success: true,
user: { id: 3, name, email }
});
});

// Start service
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Service running on port ${PORT}`);
});

RESTful API Example

const express = require('express');
const app = express();

app.use(express.json());

// Mock database
let users = [
{ id: 1, name: 'Zhang San', email: 'zhangsan@example.com' },
{ id: 2, name: 'Li Si', email: 'lisi@example.com' }
];

// Get all users
app.get('/helloworld', (req, res) => {
res.json({ success: true, users });
});

// Get single user
app.get('/helloworld/:id', (req, res) => {
const user = users.find(u => u.id === parseInt(req.params.id));

if (!user) {
return res.status(404).json({
success: false,
error: 'User not found'
});
}

res.json({ success: true, user });
});

// Create user
app.post('/helloworld', (req, res) => {
const { name, email } = req.body;

const newUser = {
id: users.length + 1,
name,
email
};

users.push(newUser);
res.status(201).json({ success: true, user: newUser });
});

// Update user
app.put('/helloworld/:id', (req, res) => {
const user = users.find(u => u.id === parseInt(req.params.id));

if (!user) {
return res.status(404).json({
success: false,
error: 'User not found'
});
}

user.name = req.body.name || user.name;
user.email = req.body.email || user.email;

res.json({ success: true, user });
});

// Delete user
app.delete('/helloworld/:id', (req, res) => {
const index = users.findIndex(u => u.id === parseInt(req.params.id));

if (index === -1) {
return res.status(404).json({
success: false,
error: 'User not found'
});
}

users.splice(index, 1);
res.json({ success: true, message: 'User deleted' });
});

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});