@@ -5,8 +5,10 @@ import (
5
5
"context"
6
6
"errors"
7
7
"io"
8
+ "iter"
8
9
"path/filepath"
9
10
"reflect"
11
+ "slices"
10
12
"strconv"
11
13
"sync"
12
14
"time"
@@ -129,6 +131,29 @@ func (r *Request) Header(key string) []string {
129
131
return r .header .PeekMultiple (key )
130
132
}
131
133
134
+ // Headers returns all headers in the request using an iterator.
135
+ // You can use maps.Collect() to collect all headers into a map.
136
+ //
137
+ // The returned value is valid until the request object is released.
138
+ // Any future calls to Headers method will return the modified value. Do not store references to returned value. Make copies instead.
139
+ func (r * Request ) Headers () iter.Seq2 [string , []string ] {
140
+ return func (yield func (string , []string ) bool ) {
141
+ keys := r .header .PeekKeys ()
142
+
143
+ for _ , key := range keys {
144
+ vals := r .header .PeekAll (utils .UnsafeString (key ))
145
+ valsStr := make ([]string , len (vals ))
146
+ for i , v := range vals {
147
+ valsStr [i ] = utils .UnsafeString (v )
148
+ }
149
+
150
+ if ! yield (utils .UnsafeString (key ), valsStr ) {
151
+ return
152
+ }
153
+ }
154
+ }
155
+ }
156
+
132
157
// AddHeader method adds a single header field and its value in the request instance.
133
158
func (r * Request ) AddHeader (key , val string ) * Request {
134
159
r .header .Add (key , val )
@@ -168,6 +193,33 @@ func (r *Request) Param(key string) []string {
168
193
return res
169
194
}
170
195
196
+ // Params returns all params in the request using an iterator.
197
+ // You can use maps.Collect() to collect all params into a map.
198
+ //
199
+ // The returned value is valid until the request object is released.
200
+ // Any future calls to Params method will return the modified value. Do not store references to returned value. Make copies instead.
201
+ func (r * Request ) Params () iter.Seq2 [string , []string ] {
202
+ return func (yield func (string , []string ) bool ) {
203
+ keys := r .params .Keys ()
204
+
205
+ for _ , key := range keys {
206
+ if key == "" {
207
+ continue
208
+ }
209
+
210
+ vals := r .params .PeekMulti (key )
211
+ valsStr := make ([]string , len (vals ))
212
+ for i , v := range vals {
213
+ valsStr [i ] = utils .UnsafeString (v )
214
+ }
215
+
216
+ if ! yield (key , valsStr ) {
217
+ return
218
+ }
219
+ }
220
+ }
221
+ }
222
+
171
223
// AddParam method adds a single param field and its value in the request instance.
172
224
func (r * Request ) AddParam (key , val string ) * Request {
173
225
r .params .Add (key , val )
@@ -254,6 +306,18 @@ func (r *Request) Cookie(key string) string {
254
306
return ""
255
307
}
256
308
309
+ // Cookies returns all cookies in the cookies using an iterator.
310
+ // You can use maps.Collect() to collect all cookies into a map.
311
+ func (r * Request ) Cookies () iter.Seq2 [string , string ] {
312
+ return func (yield func (string , string ) bool ) {
313
+ r .cookies .VisitAll (func (key , val string ) {
314
+ if ! yield (key , val ) {
315
+ return
316
+ }
317
+ })
318
+ }
319
+ }
320
+
257
321
// SetCookie method sets a single cookie field and its value in the request instance.
258
322
// It will override cookie which set in client instance.
259
323
func (r * Request ) SetCookie (key , val string ) * Request {
@@ -291,6 +355,18 @@ func (r *Request) PathParam(key string) string {
291
355
return ""
292
356
}
293
357
358
+ // PathParams returns all path params in request instance.
359
+ // You can use maps.Collect() to collect all cookies into a map.
360
+ func (r * Request ) PathParams () iter.Seq2 [string , string ] {
361
+ return func (yield func (string , string ) bool ) {
362
+ r .path .VisitAll (func (key , val string ) {
363
+ if ! yield (key , val ) {
364
+ return
365
+ }
366
+ })
367
+ }
368
+ }
369
+
294
370
// SetPathParam method sets a single path param field and its value in the request instance.
295
371
// It will override path param which set in client instance.
296
372
func (r * Request ) SetPathParam (key , val string ) * Request {
@@ -376,6 +452,33 @@ func (r *Request) FormData(key string) []string {
376
452
return res
377
453
}
378
454
455
+ // FormDatas method returns all form datas in request instance.
456
+ // You can use maps.Collect() to collect all cookies into a map.
457
+ //
458
+ // The returned value is valid until the request object is released.
459
+ // Any future calls to FormDatas method will return the modified value. Do not store references to returned value. Make copies instead.
460
+ func (r * Request ) FormDatas () iter.Seq2 [string , []string ] {
461
+ return func (yield func (string , []string ) bool ) {
462
+ keys := r .formData .Keys ()
463
+
464
+ for _ , key := range keys {
465
+ if key == "" {
466
+ continue
467
+ }
468
+
469
+ vals := r .formData .PeekMulti (key )
470
+ valsStr := make ([]string , len (vals ))
471
+ for i , v := range vals {
472
+ valsStr [i ] = utils .UnsafeString (v )
473
+ }
474
+
475
+ if ! yield (key , valsStr ) {
476
+ return
477
+ }
478
+ }
479
+ }
480
+ }
481
+
379
482
// AddFormData method adds a single form data field and its value in the request instance.
380
483
func (r * Request ) AddFormData (key , val string ) * Request {
381
484
r .formData .AddData (key , val )
@@ -435,6 +538,14 @@ func (r *Request) File(name string) *File {
435
538
return nil
436
539
}
437
540
541
+ // Files method returns all files in request instance.
542
+ //
543
+ // The returned value is valid until the request object is released.
544
+ // Any future calls to Files method will return the modified value. Do not store references to returned value. Make copies instead.
545
+ func (r * Request ) Files () []* File {
546
+ return r .files
547
+ }
548
+
438
549
// FileByPath returns file ptr store in request obj by path.
439
550
func (r * Request ) FileByPath (path string ) * File {
440
551
for _ , v := range r .files {
@@ -617,6 +728,16 @@ type QueryParam struct {
617
728
* fasthttp.Args
618
729
}
619
730
731
+ // Keys method returns all keys in the query params.
732
+ func (f * QueryParam ) Keys () []string {
733
+ keys := make ([]string , f .Len ())
734
+ f .VisitAll (func (key , value []byte ) {
735
+ keys = append (keys , utils .UnsafeString (key ))
736
+ })
737
+
738
+ return slices .Compact (keys )
739
+ }
740
+
620
741
// AddParams receive a map and add each value to param.
621
742
func (p * QueryParam ) AddParams (r map [string ][]string ) {
622
743
for k , v := range r {
@@ -747,6 +868,16 @@ type FormData struct {
747
868
* fasthttp.Args
748
869
}
749
870
871
+ // Keys method returns all keys in the form data.
872
+ func (f * FormData ) Keys () []string {
873
+ keys := make ([]string , f .Len ())
874
+ f .VisitAll (func (key , value []byte ) {
875
+ keys = append (keys , utils .UnsafeString (key ))
876
+ })
877
+
878
+ return slices .Compact (keys )
879
+ }
880
+
750
881
// AddData method is a wrapper of Args's Add method.
751
882
func (f * FormData ) AddData (key , val string ) {
752
883
f .Add (key , val )
0 commit comments