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

Change response body from text to bytea #27

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

Conversation

steve-chavez
Copy link
Member

Fixes #5.

I've checked the result by downloading an image:

select net.http_get('https://supabase.io/new/images/logo-dark.png');
\copy (select encode(body, 'hex') from net._http_response where id = 1) to 'supa.hex';
xxd -p -r supa.hex > supa.png
## open supa.png with any image viewer

(this way of getting the bytea to a file is mentioned in https://stackoverflow.com/a/6731452/4692662)

With this change, plain text media types have to be encoded. This can be done in pure SQL:

select net.http_get('https://supabase.io');
select encode(body, 'escape') from net._http_response where id = 1;
-- <!DOCTYPE html><html lang="en"....

We'd need to accommodate the SQL interface, perhaps it should handle text/* and some application/* media types for knowing when to encode.

Comment on lines +387 to +389
body_data = (bytea *) palloc(cdata->body->len + VARHDRSZ);
SET_VARSIZE(body_data, cdata->body->len + VARHDRSZ);
memcpy(VARDATA(body_data), cdata->body->data, cdata->body->len);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way of converting a char* to bytea is done in many parts of the postgres code(example).

@steve-chavez steve-chavez marked this pull request as ready for review July 28, 2021 07:09
@steve-chavez
Copy link
Member Author

We'll hold back merging this one for this release. As @olirice mentioned internally, it's clunky for the client to escape the body since it needs to handle encoding:

select
    encode((x.response).body, 'escape')::jsonb body_json
from
    net.http_collect_response(1, async:=false) x;

A new interface like the following needs to be made:

select net.http_collect_response_text(request_id:=1)
select net.http_collect_response_bin(request_id:=1)
select net.http_collect_response_json(request_id:=1)

But since the most common use case will be getting plain text responses, this can wait.

@steve-chavez steve-chavez marked this pull request as draft July 29, 2021 01:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Look into body response as bytea
2 participants