Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
0a967875
Commit
0a967875
authored
Aug 21, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #757 from smallfish/develop
Add new function SetBasicAuth, and update README
parents
ff9c8d94
6ae8bc1a
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
67 additions
and
33 deletions
+67
-33
README.md
httplib/README.md
+49
-33
httplib.go
httplib/httplib.go
+6
-0
httplib_test.go
httplib/httplib_test.go
+12
-0
No files found.
httplib/README.md
View file @
0a967875
...
...
@@ -6,53 +6,71 @@ httplib is an libs help you to curl remote url.
## GET
you can use Get to crawl data.
import "httplib"
import "
github.com/astaxie/beego/
httplib"
str, err := httplib.Get("http://beego.me/").String()
if err != nil {
t.Fatal(err)
// error
}
fmt.Println(str)
## POST
POST data to remote url
b:=
httplib.Post("http://beego.me/")
b
.Param("username","astaxie")
b
.Param("password","123456")
str, err :=
b
.String()
req :=
httplib.Post("http://beego.me/")
req
.Param("username","astaxie")
req
.Param("password","123456")
str, err :=
req
.String()
if err != nil {
t.Fatal(err)
// error
}
fmt.Println(str)
## set timeout
you can set timeout in request.default is 60 seconds.
## Set timeout
set Get timeout
:
The default timeout is
`60`
seconds, function prototype
:
httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
SetTimeout(connectTimeout, readWriteTimeout time.Duration)
Exmaple:
set post timeout:
// GET
httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
// POST
httplib.Post("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
-
first param is connectTimeout.
-
second param is readWriteTimeout
## debug
if you want to debug the request info, set the debug on
## Debug
If you want to debug the request info, set the debug on
httplib.Get("http://beego.me/").Debug(true)
## support HTTPS client
if request url is https. You can set the client support TSL:
## Set HTTP Basic Auth
str, err := Get("http://beego.me/").SetBasicAuth("user", "passwd").String()
if err != nil {
// error
}
fmt.Println(str)
## Set HTTPS
If request url is https, You can set the client support TSL:
httplib.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
more info about the tls.Config please visit http://golang.org/pkg/crypto/tls/#Config
More info about the
`tls.Config`
please visit http://golang.org/pkg/crypto/tls/#Config
## Set HTTP Version
some servers need to specify the protocol version of HTTP
httplib.Get("http://beego.me/").SetProtocolVersion("HTTP/1.1")
## Set Cookie
## set cookie
some http request need setcookie. So set it like this:
cookie := &http.Cookie{}
...
...
@@ -60,21 +78,20 @@ some http request need setcookie. So set it like this:
cookie.Value = "astaxie"
httplib.Get("http://beego.me/").SetCookie(cookie)
## upload file
httplib support mutil file upload, use
`b.PostFile()`
## Upload file
httplib support mutil file upload, use
`req.PostFile()`
b:=httplib.Post("http://beego.me/")
b.Param("username","astaxie")
b.Param("password","123456")
b.PostFile("uploadfile1", "httplib.pdf")
b.PostFile("uploadfile2", "httplib.txt")
str, err := b.String()
req := httplib.Post("http://beego.me/")
req.Param("username","astaxie")
req.PostFile("uploadfile1", "httplib.pdf")
str, err := req.String()
if err != nil {
t.Fatal(err)
// error
}
fmt.Println(str)
## set HTTP version
some servers need to specify the protocol version of HTTP
httplib.Get("http://beego.me/").SetProtocolVersion("HTTP/1.1")
\ No newline at end of file
See godoc for further documentation and examples.
*
[
godoc.org/github.com/astaxie/beego/httplib
](
https://godoc.org/github.com/astaxie/beego/httplib
)
httplib/httplib.go
View file @
0a967875
...
...
@@ -147,6 +147,12 @@ func (b *BeegoHttpRequest) Setting(setting BeegoHttpSettings) *BeegoHttpRequest
return
b
}
// SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.
func
(
b
*
BeegoHttpRequest
)
SetBasicAuth
(
username
,
password
string
)
*
BeegoHttpRequest
{
b
.
req
.
SetBasicAuth
(
username
,
password
)
return
b
}
// SetEnableCookie sets enable/disable cookiejar
func
(
b
*
BeegoHttpRequest
)
SetEnableCookie
(
enable
bool
)
*
BeegoHttpRequest
{
b
.
setting
.
EnableCookie
=
enable
...
...
httplib/httplib_test.go
View file @
0a967875
...
...
@@ -120,6 +120,18 @@ func TestWithCookie(t *testing.T) {
}
}
func
TestWithBasicAuth
(
t
*
testing
.
T
)
{
str
,
err
:=
Get
(
"http://httpbin.org/basic-auth/user/passwd"
)
.
SetBasicAuth
(
"user"
,
"passwd"
)
.
String
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
t
.
Log
(
str
)
n
:=
strings
.
Index
(
str
,
"authenticated"
)
if
n
==
-
1
{
t
.
Fatal
(
"authenticated not found in response"
)
}
}
func
TestWithUserAgent
(
t
*
testing
.
T
)
{
v
:=
"beego"
str
,
err
:=
Get
(
"http://httpbin.org/headers"
)
.
SetUserAgent
(
v
)
.
String
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment