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

使用requests库POST字符串至目标IP时“需类字节对象”报错的解决方法

Fixing the "bytes-like object is required" Error in Requests POST

Let’s break down how to correctly send your string URL via POST to that local endpoint, and fix that frustrating error.

First, the root cause: That error pops up when you’re passing a string where the request (or the server) expects a bytes object. This usually happens if you’re setting a specific content-type header that requires raw bytes, or if you’re not encoding your string properly before sending it.

Here are a few working solutions depending on what the endpoint expects:

1. Send the URL as raw text (most common scenario)

If the endpoint accepts plain text in the request body, you have two straightforward options:

Option A: Use the text parameter (Requests 2.26+)

This is the cleanest approach—Requests will automatically encode your string to UTF-8 and set the correct Content-Type: text/plain; charset=utf-8 header for you:

import requests

target_endpoint = "http://192.168.0.1/mobile/write.fcgi?serverId=1"
url_to_send = "https://example.com/your-target-url"  # Replace with your actual string URL

response = requests.post(target_endpoint, text=url_to_send)

# Verify the response
print(response.status_code)
print(response.text)

Option B: Encode the string manually

If you’re using an older version of Requests, or need a specific encoding, explicitly convert your string to bytes:

import requests

target_endpoint = "http://192.168.0.1/mobile/write.fcgi?serverId=1"
url_to_send = "https://example.com/your-target-url"

# Encode the string to bytes (UTF-8 is standard, adjust if needed)
payload = url_to_send.encode('utf-8')

# Send the request with explicit content-type header
response = requests.post(target_endpoint, data=payload, headers={'Content-Type': 'text/plain'})

# Check the server's response
print(response.status_code)
print(response.text)

2. Send the URL as form data

If the endpoint expects key-value form data (like a web form), wrap your URL in a dictionary and pass it to the data parameter:

import requests

target_endpoint = "http://192.168.0.1/mobile/write.fcgi?serverId=1"
# Replace 'url' with the actual field name the server expects (check docs if unsure)
payload = {'url': "https://example.com/your-target-url"}

response = requests.post(target_endpoint, data=payload)

# Verify success
print(response.status_code)
print(response.text)

3. If the endpoint expects JSON

If the server accepts JSON payloads, use the json parameter—Requests handles encoding and sets the Content-Type: application/json header automatically:

import requests

target_endpoint = "http://192.168.0.1/mobile/write.fcgi?serverId=1"
payload = {'url': "https://example.com/your-target-url"}

response = requests.post(target_endpoint, json=payload)

# Parse the JSON response if applicable
print(response.status_code)
print(response.json())

Quick Troubleshooting Tips

  • Double-check the endpoint’s documentation to confirm what data format it expects (plain text, form data, JSON, etc.)—this is the most critical step!
  • If errors persist, print the request details with print(response.request.headers) and print(response.request.body) to see exactly what’s being sent.
  • Ensure your local server (192.168.0.1) is running and reachable—connectivity issues can sometimes look like data format errors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:21:37