linux 服务器 http post API请求模拟小工具

最近在一现场,需要从服务器上测试到另外一个系统的api接口,因为客户机无法和友商的服务器通信,而我们的服务器是linux的,故对于post请求方式的api接口,貌似没见过get请求的api接口哦;
如何快速的测试友商提供的api是否可用,返回是否符合规范呢,原理很简单,用http库发post请求就好。

下面是用golang搞的一个简单的http_post请求小工具,为了可以一次上传多次使用,也为了简单好写,直接从文本文件读url,编码格式,以及接口参数;
每个参数占一行,以回车结尾。

package main

import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
)

func httphanderror(err error, when string) {
if err != nil {
fmt.Println(err, when)
os.Exit(1)
}
}

func mainhttppost() {
fileofconfig, _ := os.Open("config")
reader := bufio.NewReader(fileofconfig)
for {
linebytes, _, err := reader.ReadLine()
if err != nil {
if err == io.EOF {
break
} else {
fmt.Println("read file false error")
}
}
fmt.Println(string(linebytes))
}

resp, err := http.Post("http://xx.xx.xx.xx:8182/core/auth/sso_token.json", " application/x-www-form-urlencoded",
	strings.NewReader("sso_token=xxxxxxxxxx&isaid=28"))
httphanderror(err, "httppost")
defer resp.Body.Close()

bytes, err := ioutil.ReadAll(resp.Body)
httphanderror(err, "ioutil.readall")
fmt.Println(string(bytes))

}

func mainhttppost2() {
fileofconfig, _ := os.Open("config")
defer fileofconfig.Close()

reader := bufio.NewReader(fileofconfig)
var paramters []string
for {
	linebytes, _, err := reader.ReadLine()
	if err != nil {
		if err == io.EOF {
			break
		} else {
			fmt.Println("read file false error")
		}
	}
	fmt.Println(string(linebytes))
	paramters = append(paramters, string(linebytes))
}
//fmt.Println("================")
stringurl := paramters[0]
args := paramters[2]
resp, err := http.Post(stringurl, " application/x-www-form-urlencoded",
	strings.NewReader(args))
httphanderror(err, "httppost")
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
httphanderror(err, "ioutil.readall")
fmt.Println(string(bytes))

}

func main() {
//mainhttpget()
mainhttppost2()
}

posted @ 2020-05-11 10:25  开车的裁缝  阅读(1011)  评论(0编辑  收藏  举报