# Post Request
This section illuminates the procedure for creating and dispatching POST requests via Crawlbase's Smart AI Proxy service.
# Form Data
The below command displays how to send a POST request that carries form data.
- curl
- ruby
- node
- php
- python
- go
Using HTTP:
curl -H 'Content-Type: application/x-www-form-urlencoded' \ -F 'param=value' \ -X POST \ -x "http://[email protected]:8012" \ -k "http://httpbin.org/anything" Using HTTPS:
curl -H 'Content-Type: application/x-www-form-urlencoded' \ -F 'param=value' \ -X POST \ -x "https://[email protected]:8013" \ -k "http://httpbin.org/anything" require 'net/http' require 'openssl' require 'uri' # Choose either HTTP or HTTPS endpoint proxy_host = 'smartproxy.crawlbase.com' proxy_port = 8012 # Use 8013 for HTTPS 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 https = require('https'); 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, // Use 8013 for HTTPS 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'); // Use https:// and port 8013 for HTTPS curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, 'param=value'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $response = curl_exec($ch); if(curl_errno($ch)) { echo 'Error: ' . curl_error($ch); } curl_close($ch); ?> # pip install requests import requests from urllib3.exceptions import InsecureRequestWarning # Suppress only the single warning from urllib3 needed. requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) # Choose either HTTP or HTTPS endpoint proxy_url = "http://_USER_TOKEN_:@smartproxy.crawlbase.com:8012" # Use https:// and port 8013 for HTTPS proxies = { "http": proxy_url, "https": proxy_url } # Form data data = {'param': 'value'} try: response = requests.post( 'http://httpbin.org/anything', data=data, proxies=proxies, verify=False, timeout=30 ) response.raise_for_status() # Raise an exception for bad status codes print('Response Code:', response.status_code) print('Response Body:', response.text) except requests.exceptions.RequestException as e: print(f"An error occurred: {e}") package main import ( "net/http" "net/url" "strings" "fmt" "crypto/tls" ) func main() { // Create the proxy URL (use https:// and port 8013 for HTTPS) proxyURL, _ := url.Parse("http://[email protected]:8012") // Create a Transport with the proxy and insecure TLS configuration transport := &http.Transport{ Proxy: http.ProxyURL(proxyURL), TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } client := &http.Client{Transport: transport} 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 Data
To send a POST request that includes JSON data, refer to the subsequent command.
- curl
- ruby
- node
- php
- python
- go
Using HTTP:
curl -H "accept: application/json" \ --data '{"key1":"value1","key2":"value2"}' \ -X POST \ -x "http://[email protected]:8012" \ -k "http://httpbin.org/anything" Using HTTPS:
curl -H "accept: application/json" \ --data '{"key1":"value1","key2":"value2"}' \ -X POST \ -x "https://[email protected]:8013" \ -k "http://httpbin.org/anything" require 'net/http' require 'openssl' require 'uri' require 'json' # Choose either HTTP or HTTPS endpoint proxy_host = 'smartproxy.crawlbase.com' proxy_port = 8012 # Use 8013 for HTTPS 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 https = require('https'); // 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, // Use 8013 for HTTPS 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'); // Use https:// and port 8013 for HTTPS curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['key1' => 'value1', 'key2' => 'value2'])); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $response = curl_exec($ch); if(curl_errno($ch)) { echo 'Error: ' . curl_error($ch); } curl_close($ch); ?> # pip install requests import requests from urllib3.exceptions import InsecureRequestWarning # Suppress only the single warning from urllib3 needed. requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) # Choose either HTTP or HTTPS endpoint proxy_url = "http://_USER_TOKEN_:@smartproxy.crawlbase.com:8012" # Use https:// and port 8013 for HTTPS proxies = { "http": proxy_url, "https": proxy_url } # JSON data json_data = { 'key1': 'value1', 'key2': 'value2' } try: response = requests.post( 'http://httpbin.org/anything', json=json_data, # Using json parameter automatically sets Content-Type proxies=proxies, verify=False, timeout=30 ) response.raise_for_status() # Raise an exception for bad status codes print('Response Code:', response.status_code) print('Response Body:', response.text) except requests.exceptions.RequestException as e: print(f"An error occurred: {e}") package main import ( "bytes" "encoding/json" "net/http" "net/url" "fmt" "crypto/tls" ) func main() { // Create the proxy URL (use https:// and port 8013 for HTTPS) proxyURL, _ := url.Parse("http://[email protected]:8012") // Create a Transport with the proxy and insecure TLS configuration transport := &http.Transport{ Proxy: http.ProxyURL(proxyURL), TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } client := &http.Client{Transport: transport} // JSON data jsonData := map[string]string{ "key1": "value1", "key2": "value2", } jsonValue, _ := json.Marshal(jsonData) req, _ := http.NewRequest("POST", "http://httpbin.org/anything", bytes.NewBuffer(jsonValue)) req.Header.Set("Content-Type", "application/json") resp, err := client.Do(req) if err != nil { fmt.Println(err) return } defer resp.Body.Close() fmt.Println("POST request sent successfully") }