Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request modification #2667

Closed
TheAsda opened this issue Aug 26, 2022 · 3 comments
Closed

Request modification #2667

TheAsda opened this issue Aug 26, 2022 · 3 comments
Labels
feature new-http issues that would require (or benefit from) a new HTTP API

Comments

@TheAsda
Copy link

TheAsda commented Aug 26, 2022

Feature Description

We have specific authentication flow that requires to attach custom header to every request. I wrote extension that can generate header but attaching it to request is manual process. Sample:

import http from 'k6/http';
import auth from 'k6/x/auth';

export function setup() {
  auth.setup();
}

export default function () {
  const header = auth.getHeader();
  http.get('<some url>', {
    headers: {
      Auth: header,
    },
  });
}

Header has to be generated for every request so i cannot initialize it once in setup. It would be great to be able to somehow modify request before execution.

Suggested Solution (optional)

I see a couple of options here:

  1. Add ability to attach some function for modification request like
import http from 'k6/http';
import auth from 'k6/x/auth';

http.applyMiddleware((req) => {
  req.headers.Auth = auth.getHeader();
  return req;
});
  1. Add ability to affect on RoundTripper on Go level:
func init() {
	custom := Custom{
		original: ctx.Transport,
	}
	ctx.Transport = custom
}

type Custom struct {
	original http.RoundTripper
}

func (c Custom) RoundTrip(req *http.Request) (*http.Response, error) {
	header := getHeader()
	req.Header.Add("Auth", header)
	return c.original.RoundTrip(req)
}

// or

func init() {
	modules.AddRoundTripper(func(original http.RoundTripper) http.RoundTripper {
		return Custom{
			original: original,
		}
	})
}

Personally i'd prefer second option as it can give more flexibility.

@TheAsda
Copy link
Author

TheAsda commented Aug 26, 2022

Another option might be registration of custom auth method like

modules.RegisterAuth("custom", func(req *http.Request) *http.Request {
	//...
})
http.get('<some url>', { auth: 'custom' });

@na-- na-- added the new-http issues that would require (or benefit from) a new HTTP API label Sep 8, 2022
@na--
Copy link
Member

na-- commented Sep 8, 2022

Thanks for opening this issue and sorry for the slow reply! It's a known problem that was previously mentioned in #761 (and probably in other new-http issues) that we'd like to address with a new k6 HTTP API soon (#2461).

For now, the httpx JavaScript library that we host on jslib.k6.io might be sufficient for your needs. It's a wrapper around the existing k6/http API, take a look:

@imiric
Copy link
Contributor

imiric commented Mar 28, 2023

This won't be implemented in the current HTTP API, and since there's a workaround with the httpx library, I'll close this issue.

Request modification is a feature we'd like to have in the new HTTP API (initial design document), which we're starting to work on now. We're still ironing out the design and syntax, so feel free to follow the issue and document for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature new-http issues that would require (or benefit from) a new HTTP API
Projects
None yet
Development

No branches or pull requests

3 participants