postman使用教程9-點 code 按鈕生成代碼段


前言

postman 可以生成各種語言的代碼發送接口請求,對於會使用 postman 但python腳本還不熟練的小伙伴會很有幫助。
經常有小伙伴說:為什么我 postman 可以請求成功,用 python 無法請求成功?

code

postman 上接口調試沒問題后,可以點右側 code 按鈕

可以生成 HTTP 協議的請求報文, 這對排查問題非常方便

POST /api/v1/register HTTP/1.1
Host: localhost:8000
Content-Type: application/json
User-Agent: PostmanRuntime/7.13.0
Accept: */*
Cache-Control: no-cache
Postman-Token: 8586703c-68a8-445b-b532-4a1b9db14eb9,ebaee06d-9612-492a-98d5-c0333bb7ca9e
Host: 49.235.92.12:7005
accept-encoding: gzip, deflate
content-length: 68
Connection: keep-alive
cache-control: no-cache

{
    "username": "test_1620546381",
    "password": "123456"
}

生成 python 代碼段

可以選擇不同的開發語言

選python requests請求

點 Copy to Clipboard 按鈕會全部復制出來

import requests

url = "http://localhost:8000/api/v1/register"

payload = "{\n    \"username\": \"test_1620546518\",\n    \"password\": \"123456\"\n}"
headers = {
    'Content-Type': "application/json",
    'User-Agent': "PostmanRuntime/7.13.0",
    'Accept': "*/*",
    'Cache-Control': "no-cache",
    'Postman-Token': "8586703c-68a8-445b-b532-4a1b9db14eb9,155883b8-aea7-4b42-82db-d9e18b28cbce",
    'Host': "49.235.92.12:7005",
    'accept-encoding': "gzip, deflate",
    'content-length': "68",
    'Connection': "keep-alive",
    'cache-control': "no-cache"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

curl 請求

也可以生成 curl 請求

curl -X POST \
  http://localhost:8000/api/v1/register \
  -H 'Accept: */*' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json' \
  -H 'Host: 49.235.92.12:7005' \
  -H 'Postman-Token: 8586703c-68a8-445b-b532-4a1b9db14eb9,c96e777c-37f6-4e71-99c0-8dfa78ba516a' \
  -H 'User-Agent: PostmanRuntime/7.13.0' \
  -H 'accept-encoding: gzip, deflate' \
  -H 'cache-control: no-cache' \
  -H 'content-length: 68' \
  -d '{
    "username": "test_1620546615",
    "password": "123456"
}'

postman 支持的語言和框架

Language Framework
C LibCurl
C# RestSharp
cURL cURL
Dart Dart
Go http package
HTTP (Raw HTTP request)
Java OkHttp
Java Unirest
JavaScript Fetch
JavaScript jQuery
JavaScript XHR
NodeJS Axios
NodeJS Native
NodeJS Request
NodeJS Unirest
Objective-C NSURLSession
OCaml Cohttp
PHP cURL
PHP Http_Request2
PHP pecl_http
PowerShell RestMethod
Python http.client (Python 3)
Python Requests
Ruby NET::Http
Shell Httpie
Shell wget
Swift URLSession

作者-上海悠悠 blog地址 https://www.cnblogs.com/yoyoketang/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM