Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: httpx.ParseJsonBody don't support bytes field #4450

Closed
studyzy opened this issue Nov 6, 2024 · 2 comments · Fixed by #4471 or #4397
Closed

Bug: httpx.ParseJsonBody don't support bytes field #4450

studyzy opened this issue Nov 6, 2024 · 2 comments · Fixed by #4471 or #4397

Comments

@studyzy
Copy link
Contributor

studyzy commented Nov 6, 2024

Describe the bug

t.Run("bytes field", func(t *testing.T) {
		type v struct {
			Signature []byte `json:"signature,optional"`
		}
		v1 := v{
			Signature: []byte{0x01, 0xff, 0x00},
		}
		body, _ := json.Marshal(v1)
		t.Logf("body:%s", string(body))
		r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(body)))
		r.Header.Set(ContentType, header.JsonContentType)
		if assert.NoError(t, ParseJsonBody(r, &v1)) {
			assert.Greater(t, len(v1.Signature), 0)
		}

	})
  1. The error is

        requests_test.go:287: body:{"signature":"Af8A"}
     requests_test.go:290: 
         	Error Trace:	/Users/devinzeng/go/src/github.com/zeromicro/go-zero/rest/httpx/requests_test.go:290
         	Error:      	Received unexpected error:
         	            	fullName: `signature`, error: `string: `Af8A`, error: `invalid character 'A' looking for beginning of value``
         	Test:       	TestParseJsonBody/bytes_field
    

Expected behavior
No error, like json.Unmarshal

@OneThing98
Copy link

httpx.ParseJsonBody(and by extension json.Decoder) does not handle base64-encoded []byte fields by default. it is the standard convention of go. You should handle base64 decoding yourself.

@kevwan
Copy link
Contributor

kevwan commented Jan 22, 2025

Fixed.

@kevwan kevwan closed this as completed Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants