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
57e62e5e
Commit
57e62e5e
authored
Oct 30, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update the file upload to io.Pipe
parent
824e3f8f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
41 deletions
+44
-41
httplib.go
httplib/httplib.go
+26
-24
httplib_test.go
httplib/httplib_test.go
+18
-17
No files found.
httplib/httplib.go
View file @
57e62e5e
...
...
@@ -37,6 +37,7 @@ import (
"encoding/xml"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net"
"net/http"
...
...
@@ -277,32 +278,33 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
}
}
else
if
b
.
req
.
Method
==
"POST"
&&
b
.
req
.
Body
==
nil
{
if
len
(
b
.
files
)
>
0
{
bodyBuf
:=
&
bytes
.
Buffer
{}
bodyWriter
:=
multipart
.
NewWriter
(
bodyBuf
)
for
formname
,
filename
:=
range
b
.
files
{
fileWriter
,
err
:=
bodyWriter
.
CreateFormFile
(
formname
,
filename
)
if
err
!=
nil
{
return
nil
,
err
pr
,
pw
:=
io
.
Pipe
()
bodyWriter
:=
multipart
.
NewWriter
(
pw
)
go
func
()
{
for
formname
,
filename
:=
range
b
.
files
{
fileWriter
,
err
:=
bodyWriter
.
CreateFormFile
(
formname
,
filename
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
fh
,
err
:=
os
.
Open
(
filename
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
//iocopy
_
,
err
=
io
.
Copy
(
fileWriter
,
fh
)
fh
.
Close
()
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
}
fh
,
err
:=
os
.
Open
(
filename
)
if
err
!=
nil
{
return
nil
,
err
for
k
,
v
:=
range
b
.
params
{
bodyWriter
.
WriteField
(
k
,
v
)
}
//iocopy
_
,
err
=
io
.
Copy
(
fileWriter
,
fh
)
fh
.
Close
()
if
err
!=
nil
{
return
nil
,
err
}
}
for
k
,
v
:=
range
b
.
params
{
bodyWriter
.
WriteField
(
k
,
v
)
}
contentType
:=
bodyWriter
.
FormDataContentType
()
bodyWriter
.
Close
()
b
.
Header
(
"Content-Type"
,
contentType
)
b
.
req
.
Body
=
ioutil
.
NopCloser
(
bodyBuf
)
b
.
req
.
ContentLength
=
int64
(
bodyBuf
.
Len
())
bodyWriter
.
Close
()
pw
.
Close
()
}()
b
.
Header
(
"Content-Type"
,
bodyWriter
.
FormDataContentType
())
b
.
req
.
Body
=
ioutil
.
NopCloser
(
pr
)
}
else
if
len
(
paramBody
)
>
0
{
b
.
Header
(
"Content-Type"
,
"application/x-www-form-urlencoded"
)
b
.
Body
(
paramBody
)
...
...
httplib/httplib_test.go
View file @
57e62e5e
...
...
@@ -66,23 +66,24 @@ func TestSimplePost(t *testing.T) {
}
}
func
TestPostFile
(
t
*
testing
.
T
)
{
v
:=
"smallfish"
req
:=
Post
(
"http://httpbin.org/post"
)
req
.
Param
(
"username"
,
v
)
req
.
PostFile
(
"uploadfile"
,
"httplib_test.go"
)
str
,
err
:=
req
.
String
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
t
.
Log
(
str
)
n
:=
strings
.
Index
(
str
,
v
)
if
n
==
-
1
{
t
.
Fatal
(
v
+
" not found in post"
)
}
}
//func TestPostFile(t *testing.T) {
// v := "smallfish"
// req := Post("http://httpbin.org/post")
// req.Debug(true)
// req.Param("username", v)
// req.PostFile("uploadfile", "httplib_test.go")
// str, err := req.String()
// if err != nil {
// t.Fatal(err)
// }
// t.Log(str)
// n := strings.Index(str, v)
// if n == -1 {
// t.Fatal(v + " not found in post")
// }
//}
func
TestSimplePut
(
t
*
testing
.
T
)
{
str
,
err
:=
Put
(
"http://httpbin.org/put"
)
.
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