# 发布请求
本节阐明了通过 Crawlbase 的智能代理服务创建和发送 POST 请求的过程。
# 表单数据
下面的命令显示了如何发送一个 POST 请求携带 表单数据.
- 卷曲
- 红宝石
- 节点
- PHP
- 蟒蛇
- go
curl -H 'Content-Type: application/x-www-form-urlencoded' \
-F 'param=value' \
-X POST \
-x "http://[email protected]:8012" \
-k "http://httpbin.org/anything"
require 'net/http'
require 'openssl'
require 'uri'
proxy_host = 'smartproxy.crawlbase.com'
proxy_port = 8012
proxy_user = '_USER_TOKEN_'
uri = URI('http://httpbin.org/anything')
http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user)
http.use_ssl = uri.scheme == 'https'
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl?
# Form data
form_data = { 'param' => 'value' }
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(form_data)
res = http.request(request)
puts res.code, res.message, res.body
const http = require('http');
const querystring = require('querystring');
// Form data
const postData = querystring.stringify({
'param': 'value'
});
const options = {
hostname: 'httpbin.org',
port: 80,
path: '/anything',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
const proxy = {
host: 'smartproxy.crawlbase.com',
port: 8012,
auth: '_USER_TOKEN_'
};
const req = http.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
req.write(postData);
req.end();
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://httpbin.org/anything');
curl_setopt($ch, CURLOPT_PROXY, 'http://[email protected]:8012');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'param=value');
$response = curl_exec($ch);
curl_close($ch);
?>
import requests
data = {'param': 'value'}
response = requests.post(
'http://httpbin.org/anything',
data=data,
proxies={
'http': 'http://_USER_TOKEN_:@smartproxy.crawlbase.com:8012',
'https': 'http://_USER_TOKEN_:@smartproxy.crawlbase.com:8012'
}
)
print(response.content)
package main
import (
"net/http"
"net/url"
"strings"
"fmt"
)
func main() {
proxyURL, _ := url.Parse("http://[email protected]:8012")
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
data := url.Values{}
data.Set("param", "value")
req, _ := http.NewRequest("POST", "http://httpbin.org/anything", strings.NewReader(data.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("POST request sent successfully")
}
resp.Body.Close()
}
# JSON数据
发送包含以下内容的 POST 请求 JSON 数据,请参考后续命令。
- 卷曲
- 红宝石
- 节点
- PHP
- 蟒蛇
- go
curl -H "accept: application/json" \
--data '{"key1":"value1","key2":"value2"}' \
-X POST \
-x "http://[email protected]:8012" \
-k "http://httpbin.org/anything"
require 'net/http'
require 'openssl'
require 'uri'
require 'json'
proxy_host = 'smartproxy.crawlbase.com'
proxy_port = 8012
proxy_user = '_USER_TOKEN_'
uri = URI('http://httpbin.org/anything')
http = Net::HTTP.new(uri.host, uri.port, proxy_host, proxy_port, proxy_user)
http.use_ssl = uri.scheme == 'https'
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl?
# JSON data
json_data = { 'key1' => 'value1', 'key2' => 'value2' }
request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json')
request.body = json_data.to_json
res = http.request(request)
puts res.code, res.message, res.body
const http = require('http');
const querystring = require('querystring');
// JSON data
const postData = JSON.stringify({
'key1': 'value1',
'key2': 'value2'
});
const options = {
hostname: 'httpbin.org',
port: 80,
path: '/anything',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
};
const proxy = {
host: 'smartproxy.crawlbase.com',
port: 8012,
auth: '_USER_TOKEN_'
};
const req = http.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
req.write(postData);
req.end();
<?php
$ch = curl_init();
$data = array(
'key1' => 'value1',
'key2' => 'value2'
);
$jsonData = json_encode($data);
curl_setopt($ch, CURLOPT_URL, 'http://httpbin.org/anything');
curl_setopt($ch, CURLOPT_PROXY, 'http://[email protected]:8012');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($ch);
curl_close($ch);
?>
import requests
import json
data = {'key1': 'value1', 'key2': 'value2'}
response = requests.post(
'http://httpbin.org/anything',
data=json.dumps(data),
headers={'Content-Type': 'application/json'},
proxies={
'http': 'http://_USER_TOKEN_:@smartproxy.crawlbase.com:8012',
'https': 'http://_USER_TOKEN_:@smartproxy.crawlbase.com:8012'
}
)
print(response.content)
package main
import (
"net/http"
"net/url"
"strings"
"fmt"
)
func main() {
proxyURL, _ := url.Parse("http://[email protected]:8012")
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
jsonData := `{"key1":"value1","key2":"value2"}`
req, _ := http.NewRequest("POST", "http://httpbin.org/anything", strings.NewReader(jsonData))
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("POST request sent successfully")
}
resp.Body.Close()
}