Comments (3)
OK, I tracked down what is caused this, it is the absence of r.Request.GetBody
. This boils down to the same core issue as #154, #143, #141: We're trying to recreate what http.NewRequest()
does by struct
-embedding http.Request
, but IMO we should not be struct embedding it because there are so many corner cases.
Applying the following updates to the script above
diff --git a/main.go b/main.go
index 8b95934..80a972e 100644
--- a/main.go
+++ b/main.go
@@ -1,7 +1,11 @@
package main
import (
+ "bytes"
+ "encoding/json"
"fmt"
+ "io"
+ "io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
@@ -40,6 +44,10 @@ func main() {
fmt.Printf("body: %q\n\n%#v\n", string(body), res)
data := map[string]string{"token": "foo"}
+ bodyBytes, err := json.Marshal(data)
+ if err != nil {
+ panic(err)
+ }
// Do a POST.
fmt.Println("--- Doing a POST ---")
opts = []r2.Option{
@@ -51,6 +59,9 @@ func main() {
r2.OptJSONBody(data),
}
r := r2.New("", opts...)
+ r.Request.GetBody = func() (io.ReadCloser, error) {
+ return ioutil.NopCloser(bytes.NewReader(bodyBytes)), nil
+ }
body, res, err = r.Bytes()
if err != nil {
panic(err)
we get
--- Doing a GET ---
body: "Hello, client\n"
&http.Response{Status:"200 OK", StatusCode:200, Proto:"HTTP/1.1", ProtoMajor:1, ProtoMinor:1, Header:http.Header{"Content-Length":[]string{"14"}, "Content-Type":[]string{"text/plain; charset=utf-8"}, "Date":[]string{"Sat, 01 Feb 2020 06:09:57 GMT"}}, Body:(*http.bodyEOFSignal)(0xc0001da040), ContentLength:14, TransferEncoding:[]string(nil), Close:false, Uncompressed:false, Trailer:http.Header(nil), Request:(*http.Request)(0xc00019a200), TLS:(*tls.ConnectionState)(nil)}
--- Doing a POST ---
body: "Hello, client\n"
&http.Response{Status:"200 OK", StatusCode:200, Proto:"HTTP/1.1", ProtoMajor:1, ProtoMinor:1, Header:http.Header{"Content-Length":[]string{"14"}, "Content-Type":[]string{"text/plain; charset=utf-8"}, "Date":[]string{"Sat, 01 Feb 2020 06:09:57 GMT"}}, Body:(*http.bodyEOFSignal)(0xc0000d8640), ContentLength:14, TransferEncoding:[]string(nil), Close:false, Uncompressed:false, Trailer:http.Header(nil), Request:(*http.Request)(0xc00019a300), TLS:(*tls.ConnectionState)(nil)}
from go-sdk.
From the NewRequestWithContext
in the standard library:
req.GetBody = func() (io.ReadCloser, error) {
r := bytes.NewReader(buf)
return ioutil.NopCloser(r), nil
}
from go-sdk.
This was fixed by #178.
from go-sdk.
Related Issues (20)
- v2 bug: web.ViewCache() incorrectly claims to be cached after Parse failures HOT 1
- v2 logger swallows panic caused by `nil` HTTP request
- v3: `r2.OptTLSRootCAs()` has "typed nil" issue when checking for a nil transport HOT 1
- `r2` sends invalid HTTP requests if `Path` does not have a leading `/`. HOT 5
- v3: `web.Ctx.CookieDomain()` includes port
- Span type and span operation name for `oauthtrace` are generic HTTP
- [assert] Function doesn't compare equal to itself HOT 2
- Provide custom `CheckRedirect` for `r2.Request.Client`. HOT 3
- Off-by-one error in line numbers in panic output from within a goroutine
- `make dev-deps` failing to install goreleaser HOT 4
- `log.Trigger()` buries panics HOT 2
- Port internal SDK changes to public `go-sdk` HOT 1
- crypto, only KeySize 32 is supported HOT 1
- `testutil` suite run silently aborts on panics in `OptBefore` hooks HOT 1
- Outdated modules that contains volunerabilities HOT 1
- SNYK-GOLANG-GITHUBCOMJACKCPGPROTO3V2-1316251
- dependency error go sdk HOT 1
- When ever Particular key has empty value in XFCC Header, It is parsing wrongly
- selector/runes.go: isAlpha not robust
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-sdk.