Skip to content

Commit

Permalink
新版postman curl适配
Browse files Browse the repository at this point in the history
  • Loading branch information
link1st committed Jul 18, 2020
1 parent 1f530a7 commit 3525e72
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions model/curl_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,15 @@ func ParseTheFile(path string) (curl *CURL, err error) {

index = strings.Index(data, endSymbol)
if index <= -1 {
break
index = len(data)
// break
}
value = data[:index]
data = data[index+1:]
if len(data) >= index+1 {
data = data[index+1:]
} else {
data = ""
}

// 去除首尾空格
data = strings.TrimFunc(data, func(r rune) bool {
Expand All @@ -135,12 +140,17 @@ func ParseTheFile(path string) (curl *CURL, err error) {
return false
})

if key == "" {
continue
}

curl.Data[key] = append(curl.Data[key], value)

// break

}

// debug
// for key, value := range curl.Data {
// fmt.Println("key:", key, "value:", value)
// }
Expand All @@ -157,7 +167,7 @@ func (c *CURL) String() (url string) {
// GetUrl
func (c *CURL) GetUrl() (url string) {

keys := []string{"curl","--url"}
keys := []string{"curl", "--url"}
value := c.getDataValue(keys)
if len(value) <= 0 {

Expand All @@ -173,17 +183,14 @@ func (c *CURL) GetUrl() (url string) {
func (c *CURL) GetMethod() (method string) {
method = "GET"

var (
postKeys = []string{"--d", "--data", "--data-binary $", "--data-binary"}
)
value := c.getDataValue(postKeys)
body := c.GetBody()

if len(value) >= 1 {
if len(body) > 0 {
return "POST"
}

keys := []string{"-X", "--request"}
value = c.getDataValue(keys)
value := c.getDataValue(keys)

if len(value) <= 0 {

Expand Down Expand Up @@ -220,10 +227,11 @@ func (c *CURL) GetHeadersStr() string {
// 获取body
func (c *CURL) GetBody() (body string) {

keys := []string{"--data", "-d", "--data-raw", "--data-binary"}
keys := []string{"--data", "-d", "--data-urlencode", "--data-raw", "--data-binary"}
value := c.getDataValue(keys)

if len(value) <= 0 {
body = c.getPostForm()

return
}
Expand All @@ -233,3 +241,17 @@ func (c *CURL) GetBody() (body string) {

return
}

func (c *CURL) getPostForm() (body string) {
keys := []string{"--form", "-F", "--form-string"}
value := c.getDataValue(keys)

if len(value) <= 0 {

return
}

body = strings.Join(value, "&")

return
}

0 comments on commit 3525e72

Please sign in to comment.