Skip to content

feat: auto deduce content type when requesting data#49

Open
Kybxd wants to merge 4 commits intomasterfrom
auto-deduce-content-type-when-requesting-data
Open

feat: auto deduce content type when requesting data#49
Kybxd wants to merge 4 commits intomasterfrom
auto-deduce-content-type-when-requesting-data

Conversation

@Kybxd
Copy link
Collaborator

@Kybxd Kybxd commented Oct 16, 2025

@codecov
Copy link

codecov bot commented Oct 16, 2025

Codecov Report

❌ Patch coverage is 88.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.46%. Comparing base (cbe3a6b) to head (c6790f3).

Files with missing lines Patch % Lines
request.go 88.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #49      +/-   ##
==========================================
+ Coverage   80.19%   80.46%   +0.26%     
==========================================
  Files           7        7              
  Lines         409      430      +21     
==========================================
+ Hits          328      346      +18     
- Misses         59       61       +2     
- Partials       22       23       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

options.go Outdated
Comment on lines 213 to 214
// Data sets data of request body. It also deduces and sets the Content-Type
// for the input data.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add more details for deducing different content types based on data types:

  1. "application/json": struct, slice(except []byte), and map
  2. auto deduce by [http.DetectContentType]: []byte, io.Reader
  3. "text/plain": others

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines +196 to +197
default:
return plainTextType, fmt.Appendf(nil, "%v", bodyValue.Interface()), nil
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the data type is io.Reader, we should also process it well. just like: https://github.com/go-resty/resty/blob/v3/middleware.go#L428C7-L428C16

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. We check if the input data implements io.Reader first before checking its reflect.Kind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Data: handle any type gracefully based on "Content-Type" of headers

2 participants