| 1 |
// Copyright (c) 2019-2022, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package tdhttp |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"encoding/base64" |
| 12 |
"encoding/json" |
| 13 |
"encoding/xml" |
| 14 |
"errors" |
| 15 |
"io" |
| 16 |
"net/http" |
| 17 |
"net/http/httptest" |
| 18 |
"net/url" |
| 19 |
"strings" |
| 20 |
|
| 21 |
"github.com/maxatome/go-testdeep/internal/color" |
| 22 |
"github.com/maxatome/go-testdeep/internal/flat" |
| 23 |
"github.com/maxatome/go-testdeep/internal/types" |
| 24 |
) |
| 25 |
|
| 26 |
func newRequest(method string, target string, body io.Reader, params []any) (*http.Request, error) { |
| 27 |
header, qp, cookies, hook, err := collateRequestParams(params) |
| 28 |
if err != nil { |
| 29 |
return nil, err |
| 30 |
} |
| 31 |
|
| 32 |
// Parse path even when no query params to have consistent error |
| 33 |
// messages when using query params or not |
| 34 |
u, err := url.Parse(target) |
| 35 |
if err != nil { |
| 36 |
return nil, errors.New(color.Bad("target is not a valid path: %s", err)) |
| 37 |
} |
| 38 |
host := u.Host |
| 39 |
u.Host = "" |
| 40 |
u.Scheme = "" |
| 41 |
if len(qp) > 0 { |
| 42 |
if u.RawQuery != "" { |
| 43 |
u.RawQuery += "&" |
| 44 |
} |
| 45 |
u.RawQuery += qp.Encode() |
| 46 |
} |
| 47 |
target = u.String() |
| 48 |
|
| 49 |
req := httptest.NewRequest(method, target, body) |
| 50 |
|
| 51 |
for k, v := range header { |
| 52 |
req.Header[k] = append(req.Header[k], v...) |
| 53 |
} |
| 54 |
|
| 55 |
if host == "" { |
| 56 |
host = req.Header.Get("Host") |
| 57 |
} |
| 58 |
if host != "" { |
| 59 |
req.Host = host |
| 60 |
} |
| 61 |
|
| 62 |
for _, c := range cookies { |
| 63 |
req.AddCookie(c) |
| 64 |
} |
| 65 |
|
| 66 |
if hook != nil { |
| 67 |
if err = hook(req); err != nil { |
| 68 |
return nil, errors.New(color.Bad("hook failed: %s", err)) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return req, nil |
| 73 |
} |
| 74 |
|
| 75 |
func collateRequestParams(newRequestParams []any) (http.Header, url.Values, []*http.Cookie, func(*http.Request) error, error) { |
| 76 |
header := http.Header{} |
| 77 |
qp := url.Values{} |
| 78 |
var cookies []*http.Cookie |
| 79 |
var hook func(*http.Request) error |
| 80 |
|
| 81 |
newRequestParams = flat.Interfaces(newRequestParams...) |
| 82 |
for i := 0; i < len(newRequestParams); i++ { |
| 83 |
switch cur := newRequestParams[i].(type) { |
| 84 |
case string: |
| 85 |
i++ |
| 86 |
var val string |
| 87 |
if i < len(newRequestParams) { |
| 88 |
var ok bool |
| 89 |
if val, ok = newRequestParams[i].(string); !ok { |
| 90 |
return nil, nil, nil, nil, errors.New(color.Bad( |
| 91 |
`header "%s" should have a string value, not a %T (@ newRequestParams[%d])`, |
| 92 |
cur, newRequestParams[i], i)) |
| 93 |
} |
| 94 |
} |
| 95 |
header.Add(cur, val) |
| 96 |
|
| 97 |
case http.Header: |
| 98 |
for k, v := range cur { |
| 99 |
k = http.CanonicalHeaderKey(k) |
| 100 |
header[k] = append(header[k], v...) |
| 101 |
} |
| 102 |
|
| 103 |
case *http.Cookie: |
| 104 |
cookies = append(cookies, cur) |
| 105 |
|
| 106 |
case http.Cookie: |
| 107 |
cookies = append(cookies, &cur) |
| 108 |
|
| 109 |
case []*http.Cookie: |
| 110 |
cookies = append(cookies, cur...) |
| 111 |
|
| 112 |
case url.Values: |
| 113 |
for k, v := range cur { |
| 114 |
qp[k] = append(qp[k], v...) |
| 115 |
} |
| 116 |
|
| 117 |
case Q: |
| 118 |
err := cur.AddTo(qp) |
| 119 |
if err != nil { |
| 120 |
return nil, nil, nil, nil, errors.New(color.Bad( |
| 121 |
"newRequestParams... tdhttp.Q bad parameter: %s (@ newRequestParams[%d])", |
| 122 |
err, i)) |
| 123 |
} |
| 124 |
|
| 125 |
case func(*http.Request) error: |
| 126 |
hook = mergeHooks(cur, hook) |
| 127 |
|
| 128 |
default: |
| 129 |
return nil, nil, nil, nil, errors.New(color.Bad( |
| 130 |
"newRequestParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values, tdhttp.Q and func(*http.Request) error, not %T (@ newRequestParams[%d])", |
| 131 |
cur, i)) |
| 132 |
} |
| 133 |
} |
| 134 |
if len(header) == 0 { |
| 135 |
header = nil |
| 136 |
} |
| 137 |
if len(qp) == 0 { |
| 138 |
qp = nil |
| 139 |
} |
| 140 |
return header, qp, cookies, hook, nil |
| 141 |
} |
| 142 |
|
| 143 |
// BasicAuthHeader returns a new [http.Header] with only Authorization |
| 144 |
// key set, compliant with HTTP Basic Authentication using user and |
| 145 |
// password. It is provided as a facility to build request in one |
| 146 |
// line: |
| 147 |
// |
| 148 |
// ta.Get("/path", tdhttp.BasicAuthHeader("max", "5ecr3T")) |
| 149 |
// |
| 150 |
// instead of: |
| 151 |
// |
| 152 |
// req := tdhttp.Get("/path") |
| 153 |
// req.SetBasicAuth("max", "5ecr3T") |
| 154 |
// ta.Request(req) |
| 155 |
// |
| 156 |
// See [http.Request.SetBasicAuth] for details. |
| 157 |
func BasicAuthHeader(user, password string) http.Header { |
| 158 |
return http.Header{ |
| 159 |
"Authorization": []string{ |
| 160 |
"Basic " + base64.StdEncoding.EncodeToString([]byte(user+":"+password)), |
| 161 |
}, |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
func get(target string, newRequestParams ...any) (*http.Request, error) { |
| 166 |
return newRequest(http.MethodGet, target, nil, newRequestParams) |
| 167 |
} |
| 168 |
|
| 169 |
func head(target string, newRequestParams ...any) (*http.Request, error) { |
| 170 |
return newRequest(http.MethodHead, target, nil, newRequestParams) |
| 171 |
} |
| 172 |
|
| 173 |
func options(target string, body io.Reader, newRequestParams ...any) (*http.Request, error) { |
| 174 |
return newRequest(http.MethodOptions, target, body, newRequestParams) |
| 175 |
} |
| 176 |
|
| 177 |
func post(target string, body io.Reader, newRequestParams ...any) (*http.Request, error) { |
| 178 |
return newRequest(http.MethodPost, target, body, newRequestParams) |
| 179 |
} |
| 180 |
|
| 181 |
func postForm(target string, data URLValuesEncoder, newRequestParams ...any) (*http.Request, error) { |
| 182 |
var body string |
| 183 |
if data != nil { |
| 184 |
body = data.Encode() |
| 185 |
} |
| 186 |
|
| 187 |
return newRequest( |
| 188 |
http.MethodPost, target, strings.NewReader(body), |
| 189 |
append(newRequestParams, "Content-Type", "application/x-www-form-urlencoded"), |
| 190 |
) |
| 191 |
} |
| 192 |
|
| 193 |
func postMultipartFormData(target string, data *MultipartBody, newRequestParams ...any) (*http.Request, error) { |
| 194 |
return newRequest( |
| 195 |
http.MethodPost, target, data, |
| 196 |
append(newRequestParams, "Content-Type", data.ContentType()), |
| 197 |
) |
| 198 |
} |
| 199 |
|
| 200 |
func put(target string, body io.Reader, newRequestParams ...any) (*http.Request, error) { |
| 201 |
return newRequest(http.MethodPut, target, body, newRequestParams) |
| 202 |
} |
| 203 |
|
| 204 |
func patch(target string, body io.Reader, newRequestParams ...any) (*http.Request, error) { |
| 205 |
return newRequest(http.MethodPatch, target, body, newRequestParams) |
| 206 |
} |
| 207 |
|
| 208 |
func del(target string, body io.Reader, newRequestParams ...any) (*http.Request, error) { |
| 209 |
return newRequest(http.MethodDelete, target, body, newRequestParams) |
| 210 |
} |
| 211 |
|
| 212 |
// NewRequest creates a new HTTP request as [httptest.NewRequest] |
| 213 |
// does, with the ability to immediately add some header values, some |
| 214 |
// query parameters, some cookies and/or some hooks. |
| 215 |
// |
| 216 |
// Headers can be added using string pairs as in: |
| 217 |
// |
| 218 |
// req := tdhttp.NewRequest("POST", "/pdf", body, |
| 219 |
// "Content-type", "application/pdf", |
| 220 |
// "X-Test", "value", |
| 221 |
// ) |
| 222 |
// |
| 223 |
// or using [http.Header] as in: |
| 224 |
// |
| 225 |
// req := tdhttp.NewRequest("POST", "/pdf", body, |
| 226 |
// http.Header{"Content-type": []string{"application/pdf"}}, |
| 227 |
// ) |
| 228 |
// |
| 229 |
// or using [BasicAuthHeader] as in: |
| 230 |
// |
| 231 |
// req := tdhttp.NewRequest("POST", "/pdf", body, |
| 232 |
// tdhttp.BasicAuthHeader("max", "5ecr3T"), |
| 233 |
// ) |
| 234 |
// |
| 235 |
// or using [http.Cookie] (pointer or not, behind the scene, |
| 236 |
// [http.Request.AddCookie] is used) as in: |
| 237 |
// |
| 238 |
// req := tdhttp.NewRequest("POST", "/pdf", body, |
| 239 |
// http.Cookie{Name: "cook1", Value: "val1"}, |
| 240 |
// &http.Cookie{Name: "cook2", Value: "val2"}, |
| 241 |
// ) |
| 242 |
// |
| 243 |
// Several header sources are combined: |
| 244 |
// |
| 245 |
// req := tdhttp.NewRequest("POST", "/pdf", body, |
| 246 |
// "Content-type", "application/pdf", |
| 247 |
// http.Header{"X-Test": []string{"value1"}}, |
| 248 |
// "X-Test", "value2", |
| 249 |
// http.Cookie{Name: "cook1", Value: "val1"}, |
| 250 |
// tdhttp.BasicAuthHeader("max", "5ecr3T"), |
| 251 |
// &http.Cookie{Name: "cook2", Value: "val2"}, |
| 252 |
// ) |
| 253 |
// |
| 254 |
// Produces the following [http.Header]: |
| 255 |
// |
| 256 |
// http.Header{ |
| 257 |
// "Authorization": []string{"Basic bWF4OjVlY3IzVA=="}, |
| 258 |
// "Content-type": []string{"application/pdf"}, |
| 259 |
// "Cookie": []string{"cook1=val1; cook2=val2"}, |
| 260 |
// "X-Test": []string{"value1", "value2"}, |
| 261 |
// } |
| 262 |
// |
| 263 |
// A string slice or a map can be flatened as well. As [NewRequest] expects |
| 264 |
// ...any, [td.Flatten] can help here too: |
| 265 |
// |
| 266 |
// strHeaders := map[string]string{ |
| 267 |
// "X-Length": "666", |
| 268 |
// "X-Foo": "bar", |
| 269 |
// } |
| 270 |
// req := tdhttp.NewRequest("POST", "/pdf", body, td.Flatten(strHeaders)) |
| 271 |
// |
| 272 |
// Or combined with forms seen above: |
| 273 |
// |
| 274 |
// req := tdhttp.NewRequest("POST", "/pdf", body, |
| 275 |
// "Content-type", "application/pdf", |
| 276 |
// http.Header{"X-Test": []string{"value1"}}, |
| 277 |
// td.Flatten(strHeaders), |
| 278 |
// "X-Test", "value2", |
| 279 |
// http.Cookie{Name: "cook1", Value: "val1"}, |
| 280 |
// tdhttp.BasicAuthHeader("max", "5ecr3T"), |
| 281 |
// &http.Cookie{Name: "cook2", Value: "val2"}, |
| 282 |
// ) |
| 283 |
// |
| 284 |
// Header keys are always canonicalized using [http.CanonicalHeaderKey]. |
| 285 |
// |
| 286 |
// Query parameters can be added using [url.Values] or more flexible |
| 287 |
// [Q], as in: |
| 288 |
// |
| 289 |
// req := tdhttp.NewRequest("GET", "/pdf", |
| 290 |
// url.Values{ |
| 291 |
// "param": {"val"}, |
| 292 |
// "names": {"bob", "alice"}, |
| 293 |
// }, |
| 294 |
// "X-Test": "a header in the middle", |
| 295 |
// tdhttp.Q{ |
| 296 |
// "limit": 20, |
| 297 |
// "ids": []int64{456, 789}, |
| 298 |
// "details": true, |
| 299 |
// }, |
| 300 |
// ) |
| 301 |
// |
| 302 |
// All [url.Values] and [Q] instances are combined to produce the |
| 303 |
// final query string to use. The previous example produces the |
| 304 |
// following target: |
| 305 |
// |
| 306 |
// /pdf?details=true&ids=456&ids=789&limit=20&names=bob&names=alice¶m=val |
| 307 |
// |
| 308 |
// If target already contains a query string, it is reused: |
| 309 |
// |
| 310 |
// req := tdhttp.NewRequest("GET", "/pdf?limit=10", tdhttp.Q{"details": true}) |
| 311 |
// |
| 312 |
// produces the following target: |
| 313 |
// |
| 314 |
// /path?details=true&limit=10 |
| 315 |
// |
| 316 |
// Behind the scene, [url.Values.Encode] is used, so the parameters |
| 317 |
// are always sorted by key. If you want a specific order, then do not |
| 318 |
// use [url.Values] nor [Q] instances, but compose target by yourself. |
| 319 |
// |
| 320 |
// See [Q] documentation to learn how values are stringified. |
| 321 |
// |
| 322 |
// Last but not least, request hooks can be passed as |
| 323 |
// func(*http.Request) error instances. These functions are called |
| 324 |
// just before returning the [*http.Request]: |
| 325 |
// |
| 326 |
// req := tdhttp.NewRequest("GET", "/pdf", func (r *http.Request) error { |
| 327 |
// a := req.Header.Get("X-A") |
| 328 |
// b := req.Header.Get("X-A") |
| 329 |
// if a != "" && b != "" { |
| 330 |
// req.Header.Set("X-A-B", a+"-"+b) |
| 331 |
// } |
| 332 |
// }) |
| 333 |
// |
| 334 |
// If several hooks are passed, they are called in the same order as |
| 335 |
// they appear in parameters. If a hook returns an error, then |
| 336 |
// NewRequest panics. |
| 337 |
// |
| 338 |
// Hooks are useful when used with [TestAPI.DefaultRequestParams] or |
| 339 |
// [TestAPI.AddDefaultRequestParams]. |
| 340 |
func NewRequest(method, target string, body io.Reader, newRequestParams ...any) *http.Request { |
| 341 |
req, err := newRequest(method, target, body, newRequestParams) |
| 342 |
if err != nil { |
| 343 |
panic(err) |
| 344 |
} |
| 345 |
return req |
| 346 |
} |
| 347 |
|
| 348 |
// Get creates a new HTTP GET. It is a shortcut for: |
| 349 |
// |
| 350 |
// tdhttp.NewRequest(http.MethodGet, target, nil, newRequestParams...) |
| 351 |
// |
| 352 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 353 |
func Get(target string, newRequestParams ...any) *http.Request { |
| 354 |
req, err := get(target, newRequestParams...) |
| 355 |
if err != nil { |
| 356 |
panic(err) |
| 357 |
} |
| 358 |
return req |
| 359 |
} |
| 360 |
|
| 361 |
// Head creates a new HTTP HEAD. It is a shortcut for: |
| 362 |
// |
| 363 |
// tdhttp.NewRequest(http.MethodHead, target, nil, newRequestParams...) |
| 364 |
// |
| 365 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 366 |
func Head(target string, newRequestParams ...any) *http.Request { |
| 367 |
req, err := head(target, newRequestParams...) |
| 368 |
if err != nil { |
| 369 |
panic(err) |
| 370 |
} |
| 371 |
return req |
| 372 |
} |
| 373 |
|
| 374 |
// Options creates a HTTP OPTIONS. It is a shortcut for: |
| 375 |
// |
| 376 |
// tdhttp.NewRequest(http.MethodOptions, target, body, newRequestParams...) |
| 377 |
// |
| 378 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 379 |
func Options(target string, body io.Reader, newRequestParams ...any) *http.Request { |
| 380 |
req, err := options(target, body, newRequestParams...) |
| 381 |
if err != nil { |
| 382 |
panic(err) |
| 383 |
} |
| 384 |
return req |
| 385 |
} |
| 386 |
|
| 387 |
// Post creates a HTTP POST. It is a shortcut for: |
| 388 |
// |
| 389 |
// tdhttp.NewRequest(http.MethodPost, target, body, newRequestParams...) |
| 390 |
// |
| 391 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 392 |
func Post(target string, body io.Reader, newRequestParams ...any) *http.Request { |
| 393 |
req, err := post(target, body, newRequestParams...) |
| 394 |
if err != nil { |
| 395 |
panic(err) |
| 396 |
} |
| 397 |
return req |
| 398 |
} |
| 399 |
|
| 400 |
// URLValuesEncoder is an interface [PostForm] and [TestAPI.PostForm] data |
| 401 |
// must implement. |
| 402 |
// Encode can be called to generate a "URL encoded" form such as |
| 403 |
// ("bar=baz&foo=quux") sorted by key. |
| 404 |
// |
| 405 |
// [url.Values] and [Q] implement this interface. |
| 406 |
type URLValuesEncoder interface { |
| 407 |
Encode() string |
| 408 |
} |
| 409 |
|
| 410 |
// PostForm creates a HTTP POST with data's keys and values |
| 411 |
// URL-encoded as the request body. "Content-Type" header is |
| 412 |
// automatically set to "application/x-www-form-urlencoded". Other |
| 413 |
// headers can be added via newRequestParams, as in: |
| 414 |
// |
| 415 |
// req := tdhttp.PostForm("/data", |
| 416 |
// url.Values{ |
| 417 |
// "param1": []string{"val1", "val2"}, |
| 418 |
// "param2": []string{"zip"}, |
| 419 |
// }, |
| 420 |
// "X-Foo", "Foo-value", |
| 421 |
// "X-Zip", "Zip-value", |
| 422 |
// ) |
| 423 |
// |
| 424 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 425 |
func PostForm(target string, data URLValuesEncoder, newRequestParams ...any) *http.Request { |
| 426 |
req, err := postForm(target, data, newRequestParams...) |
| 427 |
if err != nil { |
| 428 |
panic(err) |
| 429 |
} |
| 430 |
return req |
| 431 |
} |
| 432 |
|
| 433 |
// PostMultipartFormData creates a HTTP POST multipart request, like |
| 434 |
// multipart/form-data one for example. See [MultipartBody] type for |
| 435 |
// details. "Content-Type" header is automatically set depending on |
| 436 |
// data.MediaType (defaults to "multipart/form-data") and data.Boundary |
| 437 |
// (defaults to "go-testdeep-42"). Other headers can be added via |
| 438 |
// newRequestParams, as in: |
| 439 |
// |
| 440 |
// req := tdhttp.PostMultipartFormData("/data", |
| 441 |
// &tdhttp.MultipartBody{ |
| 442 |
// // "multipart/form-data" by default |
| 443 |
// Parts: []*tdhttp.MultipartPart{ |
| 444 |
// tdhttp.NewMultipartPartString("type", "Sales"), |
| 445 |
// tdhttp.NewMultipartPartFile("report", "report.json", "application/json"), |
| 446 |
// }, |
| 447 |
// }, |
| 448 |
// "X-Foo", "Foo-value", |
| 449 |
// "X-Zip", "Zip-value", |
| 450 |
// ) |
| 451 |
// |
| 452 |
// and with a different media type: |
| 453 |
// |
| 454 |
// req := tdhttp.PostMultipartFormData("/data", |
| 455 |
// &tdhttp.MultipartBody{ |
| 456 |
// MediaType: "multipart/mixed", |
| 457 |
// Parts: []*tdhttp.MultipartPart{ |
| 458 |
// tdhttp.NewMultipartPartString("type", "Sales"), |
| 459 |
// tdhttp.NewMultipartPartFile("report", "report.json", "application/json"), |
| 460 |
// }, |
| 461 |
// }, |
| 462 |
// "X-Foo", "Foo-value", |
| 463 |
// "X-Zip", "Zip-value", |
| 464 |
// ) |
| 465 |
// |
| 466 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 467 |
func PostMultipartFormData(target string, data *MultipartBody, newRequestParams ...any) *http.Request { |
| 468 |
req, err := postMultipartFormData(target, data, newRequestParams...) |
| 469 |
if err != nil { |
| 470 |
panic(err) |
| 471 |
} |
| 472 |
return req |
| 473 |
} |
| 474 |
|
| 475 |
// Put creates a HTTP PUT. It is a shortcut for: |
| 476 |
// |
| 477 |
// tdhttp.NewRequest(http.MethodPut, target, body, newRequestParams...) |
| 478 |
// |
| 479 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 480 |
func Put(target string, body io.Reader, newRequestParams ...any) *http.Request { |
| 481 |
req, err := put(target, body, newRequestParams...) |
| 482 |
if err != nil { |
| 483 |
panic(err) |
| 484 |
} |
| 485 |
return req |
| 486 |
} |
| 487 |
|
| 488 |
// Patch creates a HTTP PATCH. It is a shortcut for: |
| 489 |
// |
| 490 |
// tdhttp.NewRequest(http.MethodPatch, target, body, newRequestParams...) |
| 491 |
// |
| 492 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 493 |
func Patch(target string, body io.Reader, newRequestParams ...any) *http.Request { |
| 494 |
req, err := patch(target, body, newRequestParams...) |
| 495 |
if err != nil { |
| 496 |
panic(err) |
| 497 |
} |
| 498 |
return req |
| 499 |
} |
| 500 |
|
| 501 |
// Delete creates a HTTP DELETE. It is a shortcut for: |
| 502 |
// |
| 503 |
// tdhttp.NewRequest(http.MethodDelete, target, body, newRequestParams...) |
| 504 |
// |
| 505 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 506 |
func Delete(target string, body io.Reader, newRequestParams ...any) *http.Request { |
| 507 |
req, err := del(target, body, newRequestParams...) |
| 508 |
if err != nil { |
| 509 |
panic(err) |
| 510 |
} |
| 511 |
return req |
| 512 |
} |
| 513 |
|
| 514 |
func newJSONRequest(method, target string, body any, newRequestParams ...any) (*http.Request, error) { |
| 515 |
b, err := json.Marshal(body) |
| 516 |
if err != nil { |
| 517 |
if opErr, ok := types.AsOperatorNotJSONMarshallableError(err); ok { |
| 518 |
var plus string |
| 519 |
switch op := opErr.Operator(); op { |
| 520 |
case "JSON", "SubJSONOf", "SuperJSONOf": |
| 521 |
plus = ", use json.RawMessage() instead" |
| 522 |
} |
| 523 |
return nil, errors.New(color.Bad("JSON encoding failed: %s%s", err, plus)) |
| 524 |
} |
| 525 |
return nil, errors.New(color.Bad("%s", err)) |
| 526 |
} |
| 527 |
|
| 528 |
return newRequest( |
| 529 |
method, target, bytes.NewReader(b), |
| 530 |
append(newRequestParams, "Content-Type", "application/json"), |
| 531 |
) |
| 532 |
} |
| 533 |
|
| 534 |
// NewJSONRequest creates a new HTTP request with body marshaled to |
| 535 |
// JSON. "Content-Type" header is automatically set to |
| 536 |
// "application/json". Other headers can be added via newRequestParams, as in: |
| 537 |
// |
| 538 |
// req := tdhttp.NewJSONRequest("POST", "/data", body, |
| 539 |
// "X-Foo", "Foo-value", |
| 540 |
// "X-Zip", "Zip-value", |
| 541 |
// ) |
| 542 |
// |
| 543 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 544 |
func NewJSONRequest(method, target string, body any, newRequestParams ...any) *http.Request { |
| 545 |
req, err := newJSONRequest(method, target, body, newRequestParams...) |
| 546 |
if err != nil { |
| 547 |
panic(err) |
| 548 |
} |
| 549 |
return req |
| 550 |
} |
| 551 |
|
| 552 |
// PostJSON creates a HTTP POST with body marshaled to |
| 553 |
// JSON. "Content-Type" header is automatically set to |
| 554 |
// "application/json". It is a shortcut for: |
| 555 |
// |
| 556 |
// tdhttp.NewJSONRequest(http.MethodPost, target, body, newRequestParams...) |
| 557 |
// |
| 558 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 559 |
func PostJSON(target string, body any, newRequestParams ...any) *http.Request { |
| 560 |
req, err := newJSONRequest(http.MethodPost, target, body, newRequestParams...) |
| 561 |
if err != nil { |
| 562 |
panic(err) |
| 563 |
} |
| 564 |
return req |
| 565 |
} |
| 566 |
|
| 567 |
// PutJSON creates a HTTP PUT with body marshaled to |
| 568 |
// JSON. "Content-Type" header is automatically set to |
| 569 |
// "application/json". It is a shortcut for: |
| 570 |
// |
| 571 |
// tdhttp.NewJSONRequest(http.MethodPut, target, body, newRequestParams...) |
| 572 |
// |
| 573 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 574 |
func PutJSON(target string, body any, newRequestParams ...any) *http.Request { |
| 575 |
req, err := newJSONRequest(http.MethodPut, target, body, newRequestParams...) |
| 576 |
if err != nil { |
| 577 |
panic(err) |
| 578 |
} |
| 579 |
return req |
| 580 |
} |
| 581 |
|
| 582 |
// PatchJSON creates a HTTP PATCH with body marshaled to |
| 583 |
// JSON. "Content-Type" header is automatically set to |
| 584 |
// "application/json". It is a shortcut for: |
| 585 |
// |
| 586 |
// tdhttp.NewJSONRequest(http.MethodPatch, target, body, newRequestParams...) |
| 587 |
// |
| 588 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 589 |
func PatchJSON(target string, body any, newRequestParams ...any) *http.Request { |
| 590 |
req, err := newJSONRequest(http.MethodPatch, target, body, newRequestParams...) |
| 591 |
if err != nil { |
| 592 |
panic(err) |
| 593 |
} |
| 594 |
return req |
| 595 |
} |
| 596 |
|
| 597 |
// DeleteJSON creates a HTTP DELETE with body marshaled to |
| 598 |
// JSON. "Content-Type" header is automatically set to |
| 599 |
// "application/json". It is a shortcut for: |
| 600 |
// |
| 601 |
// tdhttp.NewJSONRequest(http.MethodDelete, target, body, newRequestParams...) |
| 602 |
// |
| 603 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 604 |
func DeleteJSON(target string, body any, newRequestParams ...any) *http.Request { |
| 605 |
req, err := newJSONRequest(http.MethodDelete, target, body, newRequestParams...) |
| 606 |
if err != nil { |
| 607 |
panic(err) |
| 608 |
} |
| 609 |
return req |
| 610 |
} |
| 611 |
|
| 612 |
func newXMLRequest(method, target string, body any, newRequestParams ...any) (*http.Request, error) { |
| 613 |
b, err := xml.Marshal(body) |
| 614 |
if err != nil { |
| 615 |
return nil, errors.New(color.Bad("XML encoding failed: %s", err)) |
| 616 |
} |
| 617 |
|
| 618 |
return newRequest( |
| 619 |
method, target, bytes.NewReader(b), |
| 620 |
append(newRequestParams, "Content-Type", "application/xml"), |
| 621 |
) |
| 622 |
} |
| 623 |
|
| 624 |
// NewXMLRequest creates a new HTTP request with body marshaled to |
| 625 |
// XML. "Content-Type" header is automatically set to |
| 626 |
// "application/xml". Other headers can be added via newRequestParams, as in: |
| 627 |
// |
| 628 |
// req := tdhttp.NewXMLRequest("POST", "/data", body, |
| 629 |
// "X-Foo", "Foo-value", |
| 630 |
// "X-Zip", "Zip-value", |
| 631 |
// ) |
| 632 |
// |
| 633 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 634 |
func NewXMLRequest(method, target string, body any, newRequestParams ...any) *http.Request { |
| 635 |
req, err := newXMLRequest(method, target, body, newRequestParams...) |
| 636 |
if err != nil { |
| 637 |
panic(err) |
| 638 |
} |
| 639 |
return req |
| 640 |
} |
| 641 |
|
| 642 |
// PostXML creates a HTTP POST with body marshaled to |
| 643 |
// XML. "Content-Type" header is automatically set to |
| 644 |
// "application/xml". It is a shortcut for: |
| 645 |
// |
| 646 |
// tdhttp.NewXMLRequest(http.MethodPost, target, body, newRequestParams...) |
| 647 |
// |
| 648 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 649 |
func PostXML(target string, body any, newRequestParams ...any) *http.Request { |
| 650 |
req, err := newXMLRequest(http.MethodPost, target, body, newRequestParams...) |
| 651 |
if err != nil { |
| 652 |
panic(err) |
| 653 |
} |
| 654 |
return req |
| 655 |
} |
| 656 |
|
| 657 |
// PutXML creates a HTTP PUT with body marshaled to |
| 658 |
// XML. "Content-Type" header is automatically set to |
| 659 |
// "application/xml". It is a shortcut for: |
| 660 |
// |
| 661 |
// tdhttp.NewXMLRequest(http.MethodPut, target, body, newRequestParams...) |
| 662 |
// |
| 663 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 664 |
func PutXML(target string, body any, newRequestParams ...any) *http.Request { |
| 665 |
req, err := newXMLRequest(http.MethodPut, target, body, newRequestParams...) |
| 666 |
if err != nil { |
| 667 |
panic(err) |
| 668 |
} |
| 669 |
return req |
| 670 |
} |
| 671 |
|
| 672 |
// PatchXML creates a HTTP PATCH with body marshaled to |
| 673 |
// XML. "Content-Type" header is automatically set to |
| 674 |
// "application/xml". It is a shortcut for: |
| 675 |
// |
| 676 |
// tdhttp.NewXMLRequest(http.MethodPatch, target, body, newRequestParams...) |
| 677 |
// |
| 678 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 679 |
func PatchXML(target string, body any, newRequestParams ...any) *http.Request { |
| 680 |
req, err := newXMLRequest(http.MethodPatch, target, body, newRequestParams...) |
| 681 |
if err != nil { |
| 682 |
panic(err) |
| 683 |
} |
| 684 |
return req |
| 685 |
} |
| 686 |
|
| 687 |
// DeleteXML creates a HTTP DELETE with body marshaled to |
| 688 |
// XML. "Content-Type" header is automatically set to |
| 689 |
// "application/xml". It is a shortcut for: |
| 690 |
// |
| 691 |
// tdhttp.NewXMLRequest(http.MethodDelete, target, body, newRequestParams...) |
| 692 |
// |
| 693 |
// See [NewRequest] for all possible formats accepted in newRequestParams. |
| 694 |
func DeleteXML(target string, body any, newRequestParams ...any) *http.Request { |
| 695 |
req, err := newXMLRequest(http.MethodDelete, target, body, newRequestParams...) |
| 696 |
if err != nil { |
| 697 |
panic(err) |
| 698 |
} |
| 699 |
return req |
| 700 |
} |