• This repository has been archived on 25/Dec/2020
  • Stars
    star
    103
  • Rank 333,046 (Top 7 %)
  • Language
    Go
  • License
    Mozilla Public Li...
  • Created over 5 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🌀 Nic is a HTTP request client with elegant and easy-to-use API

Nic

GitHub release GitHub issues

English | 中文

Nic is a HTTP request client which has elegant, easy-to-use API


Features

  • wrapper of HTTP std lib, provids elegant and easy-to-use API

  • keep session via nic.Session structure, nic.Session is go-routine safe


Installation

To install nic, enter the following command

$ go get -v -u github.com/eddieivan01/nic

Quick start

Do a HTTP request like this

resp, err := nic.Get("http://example.com", nil)
if err != nil {
    log.Fatal(err.Error())
}
fmt.Println(resp.Text)

Documentation

do a basic request

nic could do these methods' request

"HEAD", "GET", "POST", "DELETE", "OPTIONS", "PUT", "PATCH"

import (
	"fmt"
    "github.com/eddieivan01/nic"
)

func main() {
    url := "http://example.com"
    resp, err := nic.Get(url, nil)
    if err != nil {
        log.Fatal(err.Error())
    }
    fmt.Println(resp.Text)
}

post request with some form data

as you see, all requests' parameters are passed by nic.H, and the inner is saved in nic.KV, it's actually map[string]interface{}

resp, err := nic.Post(url, nic.H{
    Data : nic.KV{
        "nic" : "nic",
    },
    Headers : nic.KV{
        "X-Forwarded-For" : "127.0.0.1",
    },
})

request with cookies

of course, you can also set it in Headers

resp, err := nic.Get(url, nic.H{
    Cookies : nic.KV{
        "cookie" : "nic",
    },
})

request with files

you can upload files with files' name + files' content which is []byte type, and can also upload via local file path

while uploading a file, you can set multipart form's field name, filename and MIME type

for more convenient setting files parameters, you can invoke in a chain to set filename and MIME type

resp, err := nic.Post(url, nic.H{
    Files : nic.KV{
        "file1": nic.File(
                    "nic.go", 
                    []byte("package nic")),
        "file2": nic.FileFromPath("./nic.go").
                    MIME("text/plain").
                    FName("nic"),
    },
})

request with JSON

resp, err := nic.Post(url, nic.H{
    JSON : nic.KV{
        "nic" : "nic",
    }
})

request with unencoded raw message

resp, err := nic.Post(url, nic.H{
    Raw : "post body which is unencoded",
})

using chunked transfer

The default is not to use chunked transfer

enable the transfer-encoding: chunked

resp, _ := nic.Get(url, nic.H{
    Chunked: true,
})

set query params

resp, err := nic.Get(url, nic.H {
    Params: nic.KV {
        "a": "1",
    },
})

all the parameters you could set

H struct {
    Params  KV
    Data    KV
    Raw     string
    Headers KV
    Cookies KV
    Auth    KV
    Proxy   string
    JSON    KV
    Files   KV

    AllowRedirect      bool
    Timeout            int64
    Chunked            bool
    DisableKeepAlives  bool
    DisableCompression bool
    SkipVerifyTLS      bool
}

NOTICE

nic.H can only have one of the following four parameters

H.Raw, H.Data, H.Files, H.JSON

request with session, which could handle server's set-cookie header

session := nic.NewSession()
resp, err := session.Post("http://example.com/login", nic.H{
    Data : nic.KV{
        "uname" : "nic",
        "passwd" : "nic",
    },
})

// ......

resp, err = session.Get("http://example.com/userinfo", nil)

handle response

resp, _ := nic.Get(url, nil)
fmt.Println(resp.Text)
fmt.Println(resp.Bytes)

handle JSON response

resp, _ := nic.Get(url, nil)

type S struct {
    P1 string `json:"p1"`
    P2 string `json:"p2"`
}

s := &S{}
err := resp.JSON(&s)

if err == nil {
    fmt.Println(s.P1, s.P2)
}

change response's encoding

SetEncode will convert resp.Bytes to resp.Text if encoding is changed every time be called

resp, _ := nic.Get(url, nil)
err := resp.SetEncode("gbk")

if err == nil {
    fmt.Println(resp.Text)
}

save response's content as a file

resp, _ := nic.Get("http://example.com/1.jpg", nil)
err := resp.SaveFile("1.jpg")

register a request/response hook

session := nic.NewSession()
session.RegisterBeforeReqHook(func(r *http.Request) error {
    r.URL.RawQuery = "nic=nic"
    return nil
})
session.RegisterAfterRespHook(func(r *http.Response) error {
    r.Header.Set("nic", "nic")
    return nil
})

session.Get(url, nil)

QA

  • Q:

    How to get origin *http.Request from nic.Session?

    A:

    by nic.Session.GetRequest method

  • Q:

    How to pass origin *http.Response to goquery-like DOM-parsing-libs from nic.Response?

    A:

    use resp, _ := nic.Get(...); resp.Response to access origin anonymous structure *http.Response; and (*http.Response).Body's IO.Reader has been saved, you can use *http.Response as if it were the original structure

  • Q:

    Redirection is allowed 10 times by default, how could I increase the number?

    A:

    by access nic.Session.Client then change its CheckRedirect property

  • Q:

    How to use the chunked transfer-encoding?

    A:

    by nic.H{Chunked: true}

More Repositories

1

iox

Tool for port forwarding & intranet proxy
Go
998
star
2

gld

Go shellcode LoaDer
Go
170
star
3

win32api-practice

Offensive tools written for practice purposes
C++
148
star
4

pker

Automatically converts Python source code to Pickle opcode
Python
124
star
5

memexec

A library for loading and executing PE (Portable Executable) from memory without ever touching the disk
Rust
122
star
6

secure-cookie-faker

Security tool to encode/decode Golang web-frameworks' client-side session cookie which use `gorilla/securecookie` or `gorilla/sessions`, such as Gin, Echo or Iris
Go
36
star
7

lessons-robber

CUMT公选课多线程/协程抢课脚本
Python
21
star
8

analog-login

CUMT教务系统模拟登录
Python
14
star
9

ntlmssp

Windows NTLMSSP library
Go
11
star
10

zip_crack

zip压缩文件密码暴力破解
Python
9
star
11

macho-ld

In-memory loading and executing Mach-O files
Rust
6
star
12

ctf-hash-proof

fast cli tool written for CTFer to proof hash (md5, sha1, sha256, sha512)
Go
6
star
13

tar-vuln-server

复现利用tar指令checkpoint-action参数提权的http server程序
Go
4
star
14

nemesis

cli webshell manager
Python
3
star
15

win64-syscall

Windows x64 indirect syscall lib for maldev with no_std supporting
Rust
3
star
16

async-socks5

Rust
3
star
17

EddieIvan01.github.io

SCSS
3
star
18

flask-bbs

simple BBS demo, written in Flask and Bootstrap
JavaScript
2
star
19

roarCTF-dist-casino

roarCTF challenge dist source code and writeup
Python
2
star
20

Generate_Char_By_Xor

CTF中过滤指定字符的webshell,php中由字符异或生成新字符
Python
2
star
21

Dir_Scanner_WithProxies

using proxies to scan websites' dirs
Python
1
star
22

Game

vb小游戏——生死狙击
Visual Basic
1
star
23

x-csrf

middleware to defend CSRF attack for gin framework
Go
1
star
24

ProxyPool

a simple proxy pool written in Golang
Go
1
star
25

flag

1
star
26

functional-programming

some basic data structures written in Scheme
Scheme
1
star