You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何创建发送URLEncoded格式参数的表单文本以调用短信API?

Hey there! Let's walk through how to create a form that sends URL-encoded parameters via a GET request to your SMS API. I'll cover both a basic HTML form setup and a JavaScript-enhanced version if you need more control over the encoding process.


1. Basic HTML Form (No JavaScript Needed)

The simplest approach is using a standard HTML form with method="GET" — browsers automatically encode form data as application/x-www-form-urlencoded for GET requests, which aligns perfectly with your API's requirements.

Here's the code:

<form action="YOUR_SMS_API_ENDPOINT_URL" method="GET" enctype="application/x-www-form-urlencoded">
  <div>
    <label for="username">Username:</label>
    <input type="text" id="username" name="Username" required>
  </div>
  <div>
    <label for="apikey">API Key:</label>
    <!-- Use type="password" to hide input, but note: GET will expose this in the URL! -->
    <input type="password" id="apikey" name="APIKEY" required>
  </div>
  <div>
    <label for="number">Phone Number:</label>
    <input type="tel" id="number" name="Number" required>
  </div>
  <div>
    <label for="text">Message Text:</label>
    <textarea id="text" name="Text" rows="4" required></textarea>
  </div>
  <button type="submit">Send SMS</button>
</form>
  • Replace YOUR_SMS_API_ENDPOINT_URL with your actual SMS API address.
  • When you submit this form, the browser will generate a URL like:
    https://your-api-url.com/send?Username=your-username&APIKEY=your-key&Number=1234567890&Text=Hello%20World%21
    Notice how spaces and special characters in the Text field are automatically encoded (e.g., Hello World! becomes Hello%20World%21).

2. JavaScript-Enhanced Version (For Manual Encoding Control)

If you want to modify data before submission or ensure strict encoding, use JavaScript to handle the form manually. This guarantees the Text parameter is properly URL-encoded:

<form id="smsForm">
  <div>
    <label for="username">Username:</label>
    <input type="text" id="username" name="Username" required>
  </div>
  <div>
    <label for="apikey">API Key:</label>
    <input type="password" id="apikey" name="APIKEY" required>
  </div>
  <div>
    <label for="number">Phone Number:</label>
    <input type="tel" id="number" name="Number" required>
  </div>
  <div>
    <label for="text">Message Text:</label>
    <textarea id="text" name="Text" rows="4" required></textarea>
  </div>
  <button type="submit">Send SMS</button>
</form>

<script>
document.getElementById('smsForm').addEventListener('submit', function(e) {
  e.preventDefault(); // Stop default form submission
  
  // Pull values from form fields
  const username = document.getElementById('username').value;
  const apikey = document.getElementById('apikey').value;
  const number = document.getElementById('number').value;
  const text = document.getElementById('text').value;
  
  // URL-encode all parameters (critical for special characters in Text)
  const encodedParams = new URLSearchParams({
    Username: username,
    APIKEY: apikey,
    Number: number,
    Text: text
  });
  
  // Build and navigate to the full API URL
  const apiUrl = `YOUR_SMS_API_ENDPOINT_URL?${encodedParams.toString()}`;
  window.location.href = apiUrl;
});
</script>
  • The URLSearchParams object handles proper encoding automatically, converting spaces, ampersands, question marks, and other special characters to their URL-safe equivalents. This is especially important for the Text parameter, which often contains punctuation or spaces.

3. Critical Notes to Remember
  • Security Alert: GET requests expose all parameters (including your APIKEY) in the browser's address bar, history, and server logs. If your API supports it, consider switching to POST for better security — but if GET is mandatory, be aware of this risk.
  • Input Validation: Add client-side checks (e.g., verifying phone number format) to avoid sending invalid requests to the API.
  • Test First: Submit a test message and inspect the resulting URL to confirm all parameters are correctly encoded before using the form in production.

内容的提问来源于stack exchange,提问作者Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:23:33