#HTTP Get troubles (Newbie Alert)

1 messages · Page 1 of 1 (latest)

sonic belfry
#

Or do a Google search for “http zig GitHub”

deft cedar
#

yeah but how do I install it

#

gyro doesn't work for me

#

or I haven't gotten it to work for me

pliant flint
#

Lmao 0.10.2

Zig has in master atm a http client

#

a basic one, but one

#

also in master we have the zig package manager working for packages that are updated to use it

deft cedar
pliant flint
#

Documentation

#

harder to say

#

too new atm

#

Read the source, there should be some tests you can use as examples

#

Or I think someone had a repo with an example

#

give me a minute

deft cedar
#

Alright, I appreciate you taking your time on thsi

pliant flint
#

Also disclaimer: zig http lib has less than month lol

deft cedar
#

do u mean that its less than a month old?

pliant flint
#

Yep

deft cedar
#

oh

#

I thought it was like a year-typa-new

pliant flint
#

Nop

#

It is really new

deft cedar
#

also how do I create a Allocator?

#

since .allocator = Allocator isn't working

pliant flint
#

You use an allocator implementation

deft cedar
#

would

#

allocator = std.heap.page_allocator

#

work?

pliant flint
#

yea

#

tho, keep in mind, page allocator will Out of memory quickly

deft cedar
#

Never been coding low-level languages so never had to manage my own memory like this

#

and no intellisense isn't helping

pliant flint
#

this is even a bit more than most languages

pliant flint
deft cedar
#

yup

pliant flint
#

install the ziglang zig plugin

deft cedar
#

I have but idk how to activate it or sum

pliant flint
#

ctrl + shift + p (Pallete command) And install zls

#

Tho I have to say, it works when using master

#

zls is always expecting you to use the latests version

deft cedar
#

this?

pliant flint
#

indeed

deft cedar
#

idk if I am doing this right

pliant flint
#

Curious

#

Do you have another extension for zig

#

uninstall it

deft cedar
#

i'll just uninstall all my current externsions

pliant flint
#

for zig*

deft cedar
#

yea

#

I think I did it

#

this looks correct?

#

oh wait I think my code actualyl work

#

Alright, got it working

#

imma close this thread, thanks.

pliant flint
#

hi

deft cedar
#

hello

pliant flint
#
const std = @import("std");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    var allocator = gpa.allocator();
    var http_client = std.http.Client{.allocator = allocator};
    var uri = try std.Uri.parse("https://ziglang.org/download/index.json");
    var request = try http_client.request(uri, .{}, .{});
    defer request.deinit();
    var zig_json = try request.reader().readAllAlloc(allocator, std.math.maxInt(usize));
    defer allocator.free(zig_json);
    try std.io.getStdOut().writer().writeAll(zig_json);
}


deft cedar
#

what daa

#

GeneralPurposeAllocator good one?

pliant flint
#

oh they updated zig by example

pliant flint
deft cedar
#

yeah

#

so better than page

pliant flint
#

std.testing.allocator is for tests only

#

in theory is a gpa

pliant flint
deft cedar
#

i really don't understand this .{}{} stuff

#

why

#

its like, y'all making symbols of boobs

#

mess up my brain

pliant flint
#

x33

#

An empty anonymous struct, which when passed as a parameter, it becomes (coerces) into the type the argument is asking, since there is no field to set, it's empty

deft cedar
#

is there somewhere I can get a through tutorial on this stuff such as defer, .{} stuff

pliant flint
#

reading the langref and the source😄

deft cedar
#

can u link it?

pliant flint
deft cedar
#

so wait

#

allocator.free();

#

would that just empty my memory that I allocated?

pliant flint
#

That's why I used defer

#
  • Get the gpa
  • Get the allocator
  • Create the Http Client
  • Parse the URI that we are gonna connect
  • we create the request
  • We make that at the end of the scope (in this case, at the end of the function) we remove all resources that the request allocated
  • We read all the data we are being sent and allocate it in the heap of the computer
  • We ask it to be freed once the scope ends (in this case, when the function ends)
  • We print all
deft cedar
#

but why do we need "defer" infront? what does that change

pliant flint
#

Nothing, it's a good practice

#

You could rewrite it so we free the resources at the end of the function

#

but then you have to remember that you allocated those in the first place

#

So if you allocate and instantly defer the deallocation of the resources, is easier to remember and to read what the intent is

#

And I forgot to deinit the http_client lmao

deft cedar
#

deinit, what the hell is that 🙄

#

.deint is like, neutralize it?

pliant flint
#

deinit is the name of the function that removes the resources that were allocated from the struct

deft cedar
#

so we basically destroy it

#

neutralize

#

remove

#

delete

pliant flint
#

however you want to call it

deft cedar
#

ah okay

pliant flint
#

it's not a concept, but a common name for that specific procedure

#

you init structs
deinit them

#

you create structs
you destroy them

#

Those are common names

deft cedar
#

this should work right?

pliant flint
#

in theory

#

Tho the result is not a json since it's google

deft cedar
#

that might be why its never printing the "json"

#

uh-oh

#

changed Uri and now its giving me the whole html

pliant flint
#

Yup

#

It's not gonna do more than that for ya

deft cedar
pliant flint
#

It will return you the headers, the response and not much more

deft cedar
#

this should return a http code right?

pliant flint
#

Indeed

#

tho don't try it

deft cedar
#

I tried it

#

and it aint work good

pliant flint
#

or shouuld

#

oh right, and try with {any} instead of {s}

deft cedar
#

error: access of union field 'Pointer' while field 'Enum' is active

#

bro what is it talking abt

#

that aint even my code

pliant flint
#

It's std code

#

what code triggered that

deft cedar
#

eeh idk

#

probably this

pliant flint
#
const std = @import("std");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    var allocator = gpa.allocator();

    var http_client = std.http.Client{.allocator = allocator};
    defer http_client.deinit();
    var uri = try std.Uri.parse("https://spotify.com/");
    var request = try http_client.request(uri, .{}, .{});
    defer request.deinit();
    try std.io.getStdOut().writer().print("{any}\n", .{request.response.headers.status});

    var zig_json = try request.reader().readAllAlloc(allocator, std.math.maxInt(usize));
    defer allocator.free(zig_json);
    
    try std.io.getStdOut().writer().writeAll(zig_json);
    try std.io.getStdOut().writer().print("{any}\n", .{request.response.headers.status});
}


deft cedar
#
// Allocate Memory
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    var allocator = gpa.allocator();

    var client: std.http.Client = .{ .allocator = allocator};
    defer client.deinit();

    var req = try client.request(uri, .{}, .{});
    defer req.deinit();

    var res = req.response.headers.status;

    std.debug.print("{any}", .{res});

// Release Memory
    defer allocator.free(res);
}```
#

how you make your code colored?

pliant flint
#

I use the rust syntax highlighting

deft cedar
#

no idea what that is

pliant flint
#

which is not allocated

deft cedar
#

oh

pliant flint
deft cedar
#

well, idk what to free since I am not allocating anything

#
pub fn main() !void {
// Allocate Memory
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    var allocator = gpa.allocator();

    var client: std.http.Client = .{ .allocator = allocator};
    defer client.deinit();

    var req = try client.request(uri, .{}, .{});
    defer req.deinit();

    var res = req.response.headers.status;

    std.debug.print("{any}", .{res});

// Release Memory
    defer allocator.free(res);
}```
#

thats cool

pliant flint
deft cedar
#

but then why do I need to allocate memory?

pliant flint
#

The deinits are the only things needed

pliant flint
#

that's why the client asked for a memory allocator

#

for it to be able to allocate the request and such things

deft cedar
#

so should I free that or does it free itself with the .deinit();?

pliant flint
#

It frees itself with deinit in this case

deft cedar
#
const std = @import("std");

const uri = std.Uri.parse("http://spotify.com/") catch unreachable;

pub fn main() !void {
// Allocate Memory
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    var allocator = gpa.allocator();

    var client: std.http.Client = .{ .allocator = allocator};
    defer client.deinit();

    var req = try client.request(uri, .{}, .{});
    defer req.deinit();

    var res = req.response.headers.status;

    std.debug.print("{any}", .{res});

// Don't Release Memory
}
#

this works

#

returns http.Status(682)

pliant flint
#

because you haven't read the response yet

deft cedar
#

I ain't wanna read it

#
var res = req.readAll(usize);
#

this should work

#

but it doesn't

pliant flint
#

no

#

it is asking for a buffer

deft cedar
#

eeh

pliant flint
#

So you need some array, allocated by an allocator or provided by you, to read all the things

#

var zig_json = try request.reader().readAllAlloc(allocator, std.math.maxInt(usize)); This is reading the response and all that jazz, and putting it on the heap automatically

#

if I wanted to do the same with readAll

deft cedar
#

and now

#

i would need to do this

#
defer allocator.free(res);

#

correct?

pliant flint
#

if you use readAllAlloc or readAll with an allocated buffer

#

yes

deft cedar
#
const std = @import("std");

const uri = std.Uri.parse("http://spotify.com/") catch unreachable;

pub fn main() !void {
// Allocate Memory
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    var allocator = gpa.allocator();

    var client: std.http.Client = .{ .allocator = allocator};
    defer client.deinit();

    var req = try client.request(uri, .{}, .{});
    defer req.deinit();

    var res = try req.reader().readAllAlloc(allocator, std.math.maxInt(usize));

    std.debug.print("{any}", .{res});

// Don't Release Memory
    defer allocator.free(res);
}
#

well it looks like this now

#

unfortunently it is giving me some weird stuff now

#

its an array of number

#

its not binary

#

nor hex

#

it gotta be byts

#

bytes

pliant flint
#
const std = @import("std");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    var allocator = gpa.allocator();

    var http_client = std.http.Client{.allocator = allocator};
    defer http_client.deinit();
    var uri = try std.Uri.parse("https://spotify.com/");
    var request = try http_client.request(uri, .{}, .{});
    defer request.deinit();
    try std.io.getStdOut().writer().print("{any}\n", .{request.response.headers.status});
    var buffer : [256 * 1024]u8 = undefined; // 256 kb of stack memory

    const result = try request.readAll(&buffer);
    
    try std.io.getStdOut().writer().writeAll(buffer[0..result]);
    try std.io.getStdOut().writer().print("\n{any}\n", .{request.response.headers.status});
}


deft cedar
#

ah yes. I gotta use std.io.getStdOut().writer

pliant flint
#

use {s}

#

std.format has more info on what you can do with those strings

deft cedar
#

u told me to use any!

pliant flint
deft cedar
#

aaah

#

now I see

pliant flint
#

s -> print the string
any -> try anything to print this

  • -> print the pointer
    d -> Print the number
#

and some more

deft cedar
#

I see

#

well I guess thats everything I needed to know

#

for now

#
allocator.free(res);
#

this still correct?

#

not tryna bloat my memory with shi

pliant flint
#

I mean, depends

deft cedar
pliant flint
#

Nop

deft cedar
#

what more

#

also

pliant flint
#

Stack memory is memory given to you by the OS that the compiler kinda manages

#

So when the function dies, that stack is "dead"

deft cedar
#

if I wanna free multiple stuff at the same time

allocator.free(.{1,2,3,4,5,6,7});

#

would that owkr?

#

work*?

pliant flint
#

nopp

deft cedar
#

why

pliant flint
#

First: .{1,2,3,4,5,6} is memory that the allocator did not allocate

#

each allocator has ownership of some memory

#

So an allocator from a gpa could not free memory from std.heap.page_allocator

deft cedar
#

yeah but if I like, had mutiple requests at the same time

pliant flint
#

in this case, it's allocated by the OS before the program started

pliant flint
deft cedar
#

so it would be like

#
allocator.free(1);
allocator.free(2);
pliant flint
#

Ye

#

as long those 1 and 2 are allocated by the same allocator

deft cedar
#

okay great

#

you got any good idea how to learn zig? like any youtube series or something, (else than reading the master)

pliant flint
#

Uhm, I learned like this.
But if you like learning by coding

#

give me a sec

deft cedar
#

oh

#

like a puzzle?

pliant flint
#

yep

#

it's a: fill the ???

deft cedar
#

ooh, I'll try that out

pliant flint
#

The test give you hints and teach you about how more or less the language goes

#

and you fill the gaps

deft cedar
#

alright

#

that how you learnt?

pliant flint
#

nop, but I know a lot of people that did

deft cedar
#

well I guess I gotta try it out that way then

pliant flint
#

I just went the old: Do stuff and fail a lot while reading the language reference

#

After a week of doing that for like 2-3 hours a day, I got the hang of the language

#

for std it took me longer

#

like a month

deft cedar
#

Okay, I appreciate the help and all the effort you put in to giving me all this examples and that today, Imma close this thread now and try learning more zig tomorrow.

pliant flint
#

No problem mate

deft cedar
#

😘

obtuse needle
#

I will point out that the language reference will tell you about defer, but it basically just means "Run this statement at the end of the current scope", rather than running it immediately.
This is useful because it allows you to allocate some memory, or open a file, or whathaveyou, and then immediately write the code that'll clean it up, rather than putting it at the end of the function for example:

{
    const req = getRequest();
    defer req.deinit();

    // ...

} // 'req.deinit()' is run here
#

Defers statements can be given an almost-arbitrary statement to run, or even an entire block of code.
It happens to be that the main thing it's useful for is cleaning up stuff up, but it needn't be.

deft cedar
ancient hare
#

wow, vscode now has official ziglang extension

#

cool

deft cedar
#

anyone know how I could possibly change the headers of the get request?

#

@pliant flint could you help me with this

#
const headers: std.http.Client.Request.Headers = struct {
        version: std.http.Version = .@"HTTP/1.1",
        method: std.http.Method = .GET,
    };

#

I got this far until it started going downwards