#http-odin parse to field struct

7 messages · Page 1 of 1 (latest)

solid solar
#

I don't quite get what you're asking but I'm assuming you want to marshal/unmarshal between json and struct?
If that's the case, the with_json() is already doing the struct to json.
Although in your code you're converting an empty struct...
In any case check the json package, specifically the marshalling procs...

plain charm
#

So I have a struct called Fact containing fact_number : int and fact : string and I want to make it of type Fact so that I can get the respective field like body.fact or body.fact_number

#

Instead of getting the whole api

#

Hmm and doing it to json is the problem i see

#

So instead of doing it to with_json i should just use request but then it wont accept struct as a parameter

#

🫠

plain charm
#

package shopApp

import r "vendor:raylib"
import "./http/client"
import "core:fmt"
import json "core:encoding/json"

main :: proc() {
post()
r.InitWindow(800,500,"Shop App")

for ! r.WindowShouldClose() {

r.BeginDrawing()

r.ClearBackground(r.BLACK)

drawInputBoxes(800/2 -50, 500/2 + 10,100,30)
drawInformationForBoxes()
drawInputBoxes(800/2 -50, 500/2 - 50,100,30)
drawButtonBoxes(800/2 -50, 500/2 +100,100,30)

r.EndDrawing()

}
r.CloseWindow()
}

Fact :: struct {

fact_number: int,
fact: int,

}
post :: proc() {
req: client.Request
client.request_init(&req, .Get)
defer client.request_destroy(&req)

pbody :Fact
if err := client.with_json(&req, pbody); err != nil {
    fmt.printf("JSON error: %s", err)
    return
}


res, err := client.request(&req, "https://catfacts.net/api/")
if err != nil {
    fmt.printf("Request failed: %s", err)
    return
}
defer client.response_destroy(&res)


body, allocation, berr := client.response_body(&res)
if berr != nil {
    fmt.printf("Error retrieving response body: %s", berr)
    return
}

defer client.body_destroy(body, allocation)

data: []u8
json.unmarshal_any(data, body)


fmt.println(data)

}