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

How to set headers for the gotenberg request? #26

Closed
Setti7 opened this issue Feb 6, 2021 · 5 comments
Closed

How to set headers for the gotenberg request? #26

Setti7 opened this issue Feb 6, 2021 · 5 comments
Labels
enhancement New feature or request todo

Comments

@Setti7
Copy link

Setti7 commented Feb 6, 2021

In the README there is an example on how to set the header for the request gotenberg does to an external URL, but there isn't any reference on how to set a header to the request to gotenberg itself.

Maybe if we could pass it to the gotenberg call like this?

const toPDF = pipe(
  gotenberg('https://cloud-run', { headers: { 'Authorization': 'Bearer abc' } }),
  convert,
  office,
  set(filename('result.pdf')),
  please
);
@yumauri
Copy link
Owner

yumauri commented Feb 6, 2021

Hm...

If you are using plain JavaScript, you can easily hack into pipe chain with you own function. I guess with TypeScript it will be a bit harder because of typings but should be possible as well.

For example (don't have possibility to check the code right now, but it should work):

const auth = (value) => (request) => ({
  ...request,
  headers: {
    ...request.headers,
    'Authorization': value,
  },
})

const toPDF = pipe(
  gotenberg('https://cloud-run'),
  auth('Bearer abc'),
  convert,
  office,
  set(filename('result.pdf')),
  please
);

Also:

This is undocumented, because I didn't have time then and then I just forgot ._. But gotenberg function accepts optional GotenbergClient as a second argument, and config as a third (see src).

GotenbergClient could be a plain object with fields post and optional get (get is used for ping request, so if you don't use them, you can omit it), or it could be a function, which will be called with third config argument, and should return object with fields post and optional get, or it could be a class, which will be instantiated with third config argument, and should have methods post and optional get.

You can check signature of post function here, for example.

GotenbergClient is responsible for HTTP requests to Gotenberg.
Default client is utilizing node's http and https modules, but you can create and use completely your own client with custom logic. For example, on top of got library :)

@yumauri
Copy link
Owner

yumauri commented Feb 6, 2021

Honestly I don't want to add new functionality support to default GotenbergClient, but maybe introduce more convenient way to hack into chain, for advanced usage?

For example, some sort of function adjust, which will accept any object (with proper request format) and merge it with actual request?

Like

const toPDF = pipe(
  gotenberg('https://cloud-run'),
  adjust({ headers: { 'Authorization': 'Bearer abc' } }),
  convert,
  office,
  set(filename('result.pdf')),
  please
);

Which will work exactly like code snippet in my previous comment.
It will also looks almost like your suggestion :)

What do you think?

@yumauri
Copy link
Owner

yumauri commented Feb 6, 2021

You know, I just checked my codes, and your code in your initial question even might work in some cases (maybe for URL conversions) :)

If gotenberg function's second argument doesn't have field post — it is considered as config for native GotenbergClient, and native client merges this config with actual request :)

But it will override default headers, so, I guess it will break files sent...

I think I can change this part so it will merge headers, not override them :)

@yumauri yumauri added enhancement New feature or request todo labels Feb 6, 2021
@Setti7
Copy link
Author

Setti7 commented Feb 6, 2021

The first solution worked perfectly! Thanks a lot!

The auth function in your first comment could be adapted to work just like your adjust function idea, but for me personally this solution is enough.

yumauri added a commit that referenced this issue Feb 7, 2021
yumauri added a commit that referenced this issue Feb 7, 2021
@yumauri
Copy link
Owner

yumauri commented Feb 7, 2021

Cool, but I've published new version 0.7.0 nonetheless :)

It fixes config merging and introduces new function adjust, so, with this new release your initial code should work as expected, without any changes and new handwritten functions.

As well as my code from this comment, with adjust function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request todo
Projects
None yet
Development

No branches or pull requests

2 participants