@@ -141,9 +141,14 @@ func post(target string, body io.Reader, headersQueryParams ...any) (*http.Reque
141
141
return newRequest (http .MethodPost , target , body , headersQueryParams )
142
142
}
143
143
144
- func postForm (target string , data url.Values , headersQueryParams ... any ) (* http.Request , error ) {
144
+ func postForm (target string , data URLValuesEncoder , headersQueryParams ... any ) (* http.Request , error ) {
145
+ var body string
146
+ if data != nil {
147
+ body = data .Encode ()
148
+ }
149
+
145
150
return newRequest (
146
- http .MethodPost , target , strings .NewReader (data . Encode () ),
151
+ http .MethodPost , target , strings .NewReader (body ),
147
152
append (headersQueryParams , "Content-Type" , "application/x-www-form-urlencoded" ),
148
153
)
149
154
}
@@ -336,6 +341,16 @@ func Post(target string, body io.Reader, headersQueryParams ...any) *http.Reques
336
341
return req
337
342
}
338
343
344
+ // URLValuesEncoder is an interface [PostForm] and [TestAPI.PostForm] data
345
+ // must implement.
346
+ // Encode can be called to generate a "URL encoded" form such as
347
+ // ("bar=baz&foo=quux") sorted by key.
348
+ //
349
+ // [url.Values] and [Q] implement this interface.
350
+ type URLValuesEncoder interface {
351
+ Encode () string
352
+ }
353
+
339
354
// PostForm creates a HTTP POST with data's keys and values
340
355
// URL-encoded as the request body. "Content-Type" header is
341
356
// automatically set to "application/x-www-form-urlencoded". Other
@@ -351,7 +366,7 @@ func Post(target string, body io.Reader, headersQueryParams ...any) *http.Reques
351
366
// )
352
367
//
353
368
// See [NewRequest] for all possible formats accepted in headersQueryParams.
354
- func PostForm (target string , data url. Values , headersQueryParams ... any ) * http.Request {
369
+ func PostForm (target string , data URLValuesEncoder , headersQueryParams ... any ) * http.Request {
355
370
req , err := postForm (target , data , headersQueryParams ... )
356
371
if err != nil {
357
372
panic (err )
0 commit comments