Skip to content

SetBody struct marshaling suffixing an unnecessary newline #1021

@Benjababe

Description

@Benjababe

Currently using resty.dev/v3 v3.0.0-beta.2.

I'm currently using resty to make API calls to Azure Communication Services which requires a hash calculation on my end and theirs and is then compared. However, if I pass the struct into the SetBody function, the resulting body adds a \n at the end, which changes the hash value from what is calculated on my end thus causing the request to fail.

Marshaling the struct myself is a workaround but I can see many users not realising this being the unintended cause of their issues. I was using v2 before this and never faced this issue but I'm too lazy to downgrade it and retry it.

Example:

type test struct {
	X string
	Y int
}

v := test{X: "hello", Y: 100}
resty.New().R().SetContentType("application/json").SetBody(v).Post("http://localhost:9000")

bytes, _ := json.Marshal(v)
resty.New().R().SetContentType("application/json").SetBody(bytes).Post("http://localhost:9000")

Captured request:

Server listening on port 9000...
Received body: {"X":"hello","Y":100}

Hash: TceiDSPEqgvsFjI0VGbhJ3DH6HtKANXboLpSKHUcm+8=
127.0.0.1 - - [14/May/2025 11:53:36] "POST / HTTP/1.1" 200 -
Received body: {"X":"hello","Y":100}
Hash: swxK09OTraw1Odh4d/YSbcYCXaEwg/6mc5q7dtgvlq0=
127.0.0.1 - - [14/May/2025 11:53:41] "POST / HTTP/1.1" 200 -

Note the new line on the first printed request body

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions