Skip to content

Extend the fetch function to get headers and status_code #792

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

Closed
nip1904 opened this issue Jan 27, 2025 · 3 comments · Fixed by #827
Closed

Extend the fetch function to get headers and status_code #792

nip1904 opened this issue Jan 27, 2025 · 3 comments · Fixed by #827
Labels
enhancement New feature or request

Comments

@nip1904
Copy link

nip1904 commented Jan 27, 2025

What are you building with SQLPage ?

I am fetching data with an api call.

What is your problem ? The upstream api may not always be reachable or I get a wrong response.

What are you currently doing ? I try to check the content of the json response. If i get no valid json, I have no idea what to do, to catch the exception.

I would like to extend the fetch function to receive the status_code of a response and to get reponse headers.
Then I can ask for status_code and content-type.

An alternative for me is to use the exec function an build my own try/catch or similar

A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

@nip1904 nip1904 added the enhancement New feature or request label Jan 27, 2025
@lovasoa
Copy link
Collaborator

lovasoa commented Jan 27, 2025

Hello !

Indeed a function like sqlpage.fetch_with_headers would be useful.

In the meantime, if you need to check if the response is valid json, you can do it using your database's functions. For instance, in SQLite:

set x = sqlpage.fetch('http://example.com');

select 'redirect' as component, 'error.sql' as link where not json_valid($x);

select 'text' as component, 'the json is valid' as contents;

Image

@nip1904
Copy link
Author

nip1904 commented Jan 28, 2025

HI, thank you for the hint.

lovasoa added a commit that referenced this issue Feb 22, 2025
@lovasoa
Copy link
Collaborator

lovasoa commented Feb 22, 2025

I started working on it there: https://github.com/sqlpage/SQLPage/pull/827/files

lovasoa added a commit that referenced this issue Feb 24, 2025
* new function: fetch_with_meta

closes #792

* fmt

* clippy auto

* test fetch_with_meta

* fix tests

* update deps

* retry failed deps downloads

* better json serializing

* test fetch_with_meta error handling

* add logging

* fix tests



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

Successfully merging a pull request may close this issue.

2 participants