@@ -73,12 +73,12 @@ func run(cmd *cobra.Command, args []string) {
73
73
}
74
74
75
75
func request (ctx context.Context , url string ) (* http.Request , error ) {
76
- var body io. Reader
77
- if d := opt . Data ; d != "" {
78
- body = bytes . NewReader ([] byte ( d ))
76
+ b , err := requestBody ( opt . Data )
77
+ if err != nil {
78
+ return nil , err
79
79
}
80
80
81
- req , err := http .NewRequestWithContext (ctx , opt .Method , url , body )
81
+ req , err := http .NewRequestWithContext (ctx , opt .Method , url , b )
82
82
if err != nil {
83
83
return nil , err
84
84
}
@@ -93,3 +93,23 @@ func request(ctx context.Context, url string) (*http.Request, error) {
93
93
94
94
return req , nil
95
95
}
96
+
97
+ func requestBody (data string ) (io.Reader , error ) {
98
+ if data == "" {
99
+ return nil , nil
100
+ }
101
+
102
+ if data [0 ] == '@' && len (data ) > 1 {
103
+ b , err := os .ReadFile (data [1 :])
104
+ if err != nil {
105
+ return nil , fmt .Errorf ("failed to read file: %w" , err )
106
+ }
107
+ return bytes .NewReader (removeNewline (b )), nil
108
+ }
109
+
110
+ return bytes .NewReader (removeNewline ([]byte (data ))), nil
111
+ }
112
+
113
+ func removeNewline (b []byte ) []byte {
114
+ return []byte (strings .NewReplacer ("\r \n " , "" , "\r " , "" , "\n " , "" ).Replace (string (b )))
115
+ }
0 commit comments