This repository has been archived by the owner on Jul 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀Merge pull request #2 from athul/post
🔼Post method Workings
- Loading branch information
Showing
4 changed files
with
99 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,36 @@ | ||
package methods | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
//Getreq sends a simple GET request to the url | ||
func Getreq(c *cli.Context) error { | ||
//Getbasic sends a simple GET request to the url with any potential parameters like Tokens or Basic Auth | ||
func Getbasic(c *cli.Context) error { | ||
var url = c.String("url") | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return err | ||
} | ||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
log.Println("Error on response.\n[ERRO] -", err) | ||
} | ||
defer resp.Body.Close() | ||
s := Formatresp(resp) | ||
//log.Println() | ||
fmt.Print(resp) | ||
fmt.Printf("\n\n %s", s) | ||
return nil | ||
} | ||
|
||
//Getwtoken send a get request with the Token for Authorization Header | ||
func Getwtoken(c *cli.Context) error { | ||
var url = c.String("url") | ||
var bearer = "Bearer " + c.String("token") | ||
req, err := http.NewRequest("GET", url, nil) | ||
|
||
req.Header.Add("Authorization", bearer) | ||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
log.Println("Error on response.\n[ERRO] -", err) | ||
if c.String("token") != "" { | ||
var bearer = "Bearer " + c.String("token") | ||
req.Header.Add("Authorization", bearer) | ||
} | ||
defer resp.Body.Close() | ||
s := Formatresp(resp) | ||
if s != "" { | ||
fmt.Printf("%s", s) | ||
} else { | ||
fmt.Print(resp) | ||
if c.String("u") != "" && c.String("p") != "" { | ||
un := c.String("u") | ||
pw := c.String("p") | ||
req.Header.Add("Authorization", "Basic "+basicAuth(un, pw)) | ||
} | ||
return nil | ||
} | ||
|
||
//Getbasic helps you send a request with Basic Auth as Authorization Method | ||
func Getbasic(c *cli.Context) error { | ||
un := c.String("u") | ||
pw := c.String("p") | ||
url := c.String("url") | ||
req, err := http.NewRequest("GET", url, nil) | ||
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 := Formatresp(resp) | ||
if s != "" { | ||
fmt.Printf("%s", s) | ||
} else { | ||
fmt.Print(resp) | ||
} | ||
|
||
s := formatresp(resp) | ||
fmt.Printf("\n%s", s) | ||
return nil | ||
} | ||
func basicAuth(username, password string) string { | ||
auth := username + ":" + password | ||
return base64.StdEncoding.EncodeToString([]byte(auth)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package methods | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
//Postbasic sends a basic POST request | ||
func Postbasic(c *cli.Context) { | ||
url := c.String("url") | ||
var jsonStr = []byte(c.String("body")) | ||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) | ||
//req.Header.Set("X-Custom-Header", "myvalue") | ||
req.Header.Set("Content-Type", c.String("ctype")) | ||
if c.String("token") != "" { | ||
var bearer = "Bearer " + c.String("token") | ||
req.Header.Add("Authorization", bearer) | ||
} | ||
if c.String("u") != "" && c.String("p") != "" { | ||
un := c.String("u") | ||
pw := c.String("p") | ||
req.Header.Add("Authorization", "Basic "+basicAuth(un, pw)) | ||
} | ||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer resp.Body.Close() | ||
s := formatresp(resp) | ||
fmt.Println("response Body:", s) | ||
} |