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

Commit

Permalink
✨Added Easier Content Type Setting
Browse files Browse the repository at this point in the history
  • Loading branch information
athul committed Feb 11, 2020
1 parent fe137f9 commit ae46654
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func main() {
Usage: "Add the Password",
},
cli.StringFlag{
Name: "ctype, c",
Value: "application/json",
Name: "ctype, c",
//Value: "application/json",
Usage: "Change the Content Type",
},
cli.StringFlag{
Expand Down
2 changes: 1 addition & 1 deletion methods/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Deletebasic(c *cli.Context) error {
var jsonStr = []byte(c.String("body"))
req, err := http.NewRequest("DELETE", url, bytes.NewBuffer(jsonStr))
//req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", c.String("ctype"))
req.Header.Set("Content-Type", Contenttypes[c.String("ctype")])
if c.String("token") != "" {
var bearer = "Bearer " + c.String("token")
req.Header.Add("Authorization", bearer)
Expand Down
8 changes: 8 additions & 0 deletions methods/fns.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}

//Contenttypes can be used in Place for ctypes
var Contenttypes = map[string]string{
"html": "text/html",
"js": "application/json",
"xml": "application/xml",
"plain": "text/plain",
}
2 changes: 1 addition & 1 deletion methods/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Patchbasic(c *cli.Context) {
var jsonStr = []byte(c.String("body"))
req, err := http.NewRequest("PATCH", url, bytes.NewBuffer(jsonStr))
//req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", c.String("ctype"))
req.Header.Set("Content-Type", Contenttypes[c.String("ctype")])
if c.String("token") != "" {
var bearer = "Bearer " + c.String("token")
req.Header.Add("Authorization", bearer)
Expand Down
4 changes: 3 additions & 1 deletion methods/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func Postbasic(c *cli.Context) {
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"))
req.Header.Set("Content-Type", Contenttypes[c.String("ctype")])
fmt.Print(Contenttypes[c.String("ctype")] + "\n")
if c.String("token") != "" {
var bearer = "Bearer " + c.String("token")
req.Header.Add("Authorization", bearer)
Expand All @@ -29,6 +30,7 @@ func Postbasic(c *cli.Context) {
pw := c.String("p")
req.Header.Add("Authorization", "Basic "+basicAuth(un, pw))
}
fmt.Print(req.Header)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion methods/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Putbasic(c *cli.Context) error {
var jsonStr = []byte(c.String("body"))
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonStr))
//req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", c.String("ctype"))
req.Header.Set("Content-Type", Contenttypes[c.String("ctype")])
if c.String("token") != "" {
var bearer = "Bearer " + c.String("token")
req.Header.Add("Authorization", bearer)
Expand Down

0 comments on commit ae46654

Please sign in to comment.