Giter Club home page Giter Club logo

Comments (3)

dhermes avatar dhermes commented on July 26, 2024

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.

dhermes avatar dhermes commented on July 26, 2024

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.

dhermes avatar dhermes commented on July 26, 2024

This was fixed by #178.

from go-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.