-
Notifications
You must be signed in to change notification settings - Fork 1
feat: auto deduce content type when requesting data #49
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import ( | |
| "io" | ||
| "mime/multipart" | ||
| "net/http" | ||
| "reflect" | ||
|
|
||
| "github.com/Wenchy/requests/internal/auth" | ||
| ) | ||
|
|
@@ -82,14 +83,16 @@ func request(c *Client, method, url string, opts *Options) (*Response, error) { | |
| func requestData(c *Client, method, url string, opts *Options) (*Response, error) { | ||
| body := bytes.NewBuffer(nil) | ||
| if opts.Data != nil { | ||
| d := fmt.Sprintf("%v", opts.Data) | ||
| _, err := body.WriteString(d) | ||
| contentType, bytes, err := deduceContentTypeAndBody(opts.Data) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| _, err = body.Write(bytes) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| opts.Headers.Set("Content-Type", contentType) | ||
| } | ||
| // TODO: judge content type | ||
| // opts.Headers["Content-Type"] = "application/x-www-form-urlencoded" | ||
| opts.Body = body | ||
| return c.request(method, url, opts, body.Bytes()) | ||
| } | ||
|
|
@@ -105,7 +108,7 @@ func requestForm(c *Client, method, url string, opts *Options) (*Response, error | |
| return nil, err | ||
| } | ||
| } | ||
| opts.Headers.Set("Content-Type", "application/x-www-form-urlencoded") | ||
| opts.Headers.Set("Content-Type", formContentType) | ||
| opts.Body = body | ||
| return c.request(method, url, opts, body.Bytes()) | ||
| } | ||
|
|
@@ -123,7 +126,7 @@ func requestJSON(c *Client, method, url string, opts *Options) (*Response, error | |
| return nil, err | ||
| } | ||
| } | ||
| opts.Headers.Set("Content-Type", "application/json") | ||
| opts.Headers.Set("Content-Type", jsonContentType) | ||
| opts.Body = body | ||
| return c.request(method, url, opts, body.Bytes()) | ||
| } | ||
|
|
@@ -171,3 +174,26 @@ var dispatchers map[bodyType]dispatcher = map[bodyType]dispatcher{ | |
| bodyTypeJSON: requestJSON, | ||
| bodyTypeFiles: requestFiles, | ||
| } | ||
|
|
||
| var ( | ||
| plainTextType = "text/plain; charset=utf-8" | ||
| jsonContentType = "application/json" | ||
| formContentType = "application/x-www-form-urlencoded" | ||
| ) | ||
|
|
||
| // deduceContentTypeAndBody parses content type and request body from request data | ||
| func deduceContentTypeAndBody(data any) (string, []byte, error) { | ||
| bodyValue := reflect.Indirect(reflect.ValueOf(data)) | ||
| switch bodyValue.Kind() { | ||
| case reflect.Struct, reflect.Map, reflect.Slice: | ||
| // check slice here to differentiate between any slice vs byte slice | ||
| if body, ok := data.([]byte); ok { | ||
| return http.DetectContentType(body), body, nil | ||
| } else { | ||
| body, err := json.Marshal(data) | ||
| return jsonContentType, body, err | ||
| } | ||
| default: | ||
| return plainTextType, fmt.Appendf(nil, "%v", bodyValue.Interface()), nil | ||
|
Comment on lines
+200
to
+201
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the data type is
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. We check if the input data implements |
||
| } | ||
| } | ||
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done