問題描述
Windows無法直接通過curl發送GET、POST請求
安裝
- 下載 curl for Windows,解壓到C盤
- 新建環境變量,變量名
CURL_HOME
- Path添加:
;%CURL_HOME%\I386;
- 查看版本:
curl --version
- 測試:
curl www.baidu.com
基本使用
獲取百度首頁
curl http://www.baidu.com/
獲取funet ftp服務器上的README
curl ftp://ftp.funet.fi/README
更多用法查看curl - Tutorial
GET請求
用Tornado構建API用於GET、POST請求測試
import tornado.web
import tornado.ioloop
class MainHandler(tornado.web.RequestHandler):
def get(self):
name = self.get_argument('name', default='')
phone = self.get_argument('phone', default='')
self.write('GET {} {}'.format(name, phone))
def post(self):
name = self.get_argument('name', default='')
phone = self.get_argument('phone', default='')
self.write('POST {} {}'.format(name, phone))
if __name__ == '__main__':
print('curl "http://localhost:5555/?name=Xiao%20ming&phone=13000000000"') # GET
print('curl -d "name=Xiao%20ming&phone=13000000000" http://localhost:5555/') # POST
print('curl -F "name=Xiao ming" -F "phone=13000000000" http://localhost:5555/') # POST
app = tornado.web.Application([
(r'/', MainHandler),
])
app.listen(5555)
tornado.ioloop.IOLoop.current().start()
GET請求
curl "http://localhost:5555/?name=Xiao%20ming&phone=13000000000"
POST請求
-d
:接受urlencode後的字符串
curl -d "name=Xiao%20ming&phone=13000000000" http://localhost:5555/
-F
:模擬表單
curl -F "name=Xiao ming" -F "phone=13000000000" http://localhost:5555/
擴展閱讀
- Postman——API開發協作平臺
- 需要中文嘗試——ApiPost