Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Commit

Permalink
Made it Working
Browse files Browse the repository at this point in the history
  • Loading branch information
athul committed Jan 31, 2020
1 parent c75a2a7 commit a3bb359
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 31 deletions.
24 changes: 0 additions & 24 deletions methods/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,3 @@ func Getbasic(c *cli.Context) error {
fmt.Println(s)
return nil
}
func getsend(c []Colls, ind int, method string) (string, error) {
var url = c[0].Request[ind].URL + c[0].Request[ind].Path
req, err := http.NewRequest(method, url, nil)
if err != nil {
return "", err
}
if c[0].Request[ind].Token != "" {
var bearer = "Bearer " + c[0].Request[ind].Token
req.Header.Add("Authorization", bearer)
}
if c[0].Request[ind].User != "" && c[0].Request[ind].Pass != "" {
un := c[0].Request[ind].User
pw := c[0].Request[ind].Pass
req.Header.Add("Authorization", "Basic "+basicAuth(un, pw))
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
defer resp.Body.Close()
s := fmt.Sprintf("\nStatus:\t\t%s\n\nStatusCode:\t%d\n", resp.Status, resp.StatusCode)
return s, nil
}
87 changes: 80 additions & 7 deletions methods/json.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package methods

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"

"github.com/fatih/color"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -50,24 +54,93 @@ func ReadCollection(c *cli.Context) {
fmt.Println(err)
}
for i := 0; i < len(jsondat[0].Request); i++ {
fmt.Printf(`
URL: %s%s
/* fmt.Printf(`
URL: %s
Method: %s
Auth: %s
-------`, jsondat[0].Request[i].URL, jsondat[0].Request[i].Path, jsondat[0].Request[i].Method, jsondat[0].Request[i].Auth)
Token:%s
Headers: %s
-------`, jsondat[0].Request[i].URL+jsondat[0].Request[i].Path, jsondat[0].Request[i].Method, jsondat[0].Request[i].Auth, jsondat[0].Request[i].Token, jsondat[0].Request[i].Heads) */
request(jsondat, i)
}

}
func request(c []Colls, i int) {
if c[0].Request[i].Method == "GET" || c[0].Request[i].Method == "POST" {
//fmt.Print(c[0].Request[i].Method)
out, err := getsend(c, i, c[0].Request[i].Method)
colors := color.New(color.FgHiRed, color.Bold)
if c[0].Request[i].Method == "GET" {
out, err := getsend(c, i, "GET")
if err != nil {
fmt.Print(err)
}
methods := color.HiYellowString(c[0].Request[i].Method)
fURL := colors.Sprintf(c[0].Request[i].URL + c[0].Request[i].Path)
fmt.Printf("%s \t%s\t%s", fURL, methods, out)
} else {
out, err := sendpopa(c, i, c[0].Request[i].Method)
if err != nil {
fmt.Print(err)
}
methods := color.HiYellowString(c[0].Request[i].Method)
fURL := colors.Sprintf(c[0].Request[i].URL + c[0].Request[i].Path)
fmt.Printf("%s \t%s\t%s", fURL, methods, out)
}

}
func getsend(c []Colls, ind int, method string) (string, error) {
color := color.New(color.FgCyan, color.Bold)
var url = c[0].Request[ind].URL + c[0].Request[ind].Path
req, err := http.NewRequest(method, url, nil)
if err != nil {
return "", err
}
if c[0].Request[ind].Token != "" {
var bearer = "Bearer " + c[0].Request[ind].Token
req.Header.Add("Authorization", bearer)
}
if c[0].Request[ind].User != "" && c[0].Request[ind].Pass != "" {
un := c[0].Request[ind].User
pw := c[0].Request[ind].Pass
req.Header.Add("Authorization", "Basic "+basicAuth(un, pw))
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
defer resp.Body.Close()
//fmt.Print(resp.Header)
s := color.Sprintf("Status: %s\tStatusCode:\t%d\n", resp.Status, resp.StatusCode)
return s, nil
}

func sendpopa(c []Colls, ind int, method string) (string, error) {
color := color.New(color.FgCyan, color.Bold)

var url = c[0].Request[ind].URL + c[0].Request[ind].Path
var jsonStr = []byte(string(c[0].Request[ind].Bparams[0].Key[0] + c[0].Request[ind].Bparams[0].Value[0]))

fmt.Printf("%s %d", out, i)
req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", c[0].Request[ind].Ctype)
if err != nil {
return "", err
}
if c[0].Request[ind].Token != "" {
var bearer = "Bearer " + c[0].Request[ind].Token
req.Header.Add("Authorization", bearer)
}
if c[0].Request[ind].User != "" && c[0].Request[ind].Pass != "" {
un := c[0].Request[ind].User
pw := c[0].Request[ind].Pass
req.Header.Add("Authorization", "Basic "+basicAuth(un, pw))
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
defer resp.Body.Close()
//fmt.Print(resp.Header)
s := color.Sprintf("Status: %s\tStatusCode:\t%d\n", resp.Status, resp.StatusCode)
return s, nil

}

0 comments on commit a3bb359

Please sign in to comment.