| 309 | } |
| 310 | |
| 311 | func apiRun(opts *ApiOptions) error { |
| 312 | params, err := parseFields(opts) |
| 313 | if err != nil { |
| 314 | return err |
| 315 | } |
| 316 | |
| 317 | isGraphQL := opts.RequestPath == "graphql" |
| 318 | requestPath, err := fillPlaceholders(opts.RequestPath, opts) |
| 319 | if err != nil { |
| 320 | return fmt.Errorf("unable to expand placeholder in path: %w", err) |
| 321 | } |
| 322 | method := opts.RequestMethod |
| 323 | requestHeaders := opts.RequestHeaders |
| 324 | var requestBody any |
| 325 | if len(params) > 0 { |
| 326 | requestBody = params |
| 327 | } |
| 328 | |
| 329 | if !opts.RequestMethodPassed && (len(params) > 0 || opts.RequestInputFile != "") { |
| 330 | method = "POST" |
| 331 | } |
| 332 | |
| 333 | if !opts.Silent { |
| 334 | if err := opts.IO.StartPager(); err == nil { |
| 335 | defer opts.IO.StopPager() |
| 336 | } else { |
| 337 | fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err) |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | // Response content funnels through ContentOut. It stays in passthrough here: |
| 342 | // JSON is sanitized by the transport and the jq/template/jsoncolor paths emit |
| 343 | // our own formatting, so only a raw non-JSON body needs neutralizing, done at |
| 344 | // its copy below. |
| 345 | opts.IO.SetContentSanitization(false) |
| 346 | |
| 347 | var bodyWriter io.Writer = opts.IO.ContentOut |
| 348 | var headersWriter io.Writer = opts.IO.Out |
| 349 | if opts.Silent { |
| 350 | bodyWriter = io.Discard |
| 351 | } |
| 352 | if opts.Verbose { |
| 353 | // httpClient handles output when verbose flag is specified. |
| 354 | bodyWriter = io.Discard |
| 355 | headersWriter = io.Discard |
| 356 | } |
| 357 | |
| 358 | if opts.Paginate && !isGraphQL { |
| 359 | requestPath = addPerPage(requestPath, 100, params) |
| 360 | } |
| 361 | |
| 362 | // Similar to `jq --slurp`, write all pages JSON arrays or objects into a JSON array. |
| 363 | if opts.Paginate && opts.Slurp { |
| 364 | w := &jsonArrayWriter{ |
| 365 | Writer: bodyWriter, |
| 366 | color: opts.IO.ColorEnabled(), |
| 367 | } |
| 368 | defer w.Close() |