Skip to content

Make a http request from wasi #4540

Open
@dshemin

Description

@dshemin

Hi!

I try to make a http request from WASM module running on wazero and got an error^

Netdev not set

I try to use netdev package but can't find any suitable examples. Can someone tell me is it possible or not?

My module:

package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"net/http"
	"os"
	"time"

	"tinygo.org/x/drivers/netdev"
)

//go:generate easyjson main.go

//export call
func call() {
	netdev.UseNetdev(/* That i should pass here? */)
	resp, err := http.Get("https://swapi.dev/api/people/1")
	handleErr(err, "get")

	if resp.StatusCode != http.StatusOK {
		handleErr(errors.New("not 200"), "check status code")
	}
	defer resp.Body.Close()

	var p People
	err = json.NewDecoder(resp.Body).Decode(&p)
	handleErr(err, "decode")

	fmt.Printf("%#v", p)
}

func handleErr(err error, msg string) {
	if err != nil {
		fmt.Println(msg + ": " + err.Error())
		os.Exit(1)
	}
}

//easyjson:json
type People struct {
	Name      string    `json:"name"`
	Height    string    `json:"height"`
	Mass      string    `json:"mass"`
	HairColor string    `json:"hair_color"`
	SkinColor string    `json:"skin_color"`
	EyeColor  string    `json:"eye_color"`
	BirthYear string    `json:"birth_year"`
	Gender    string    `json:"gender"`
	Homeworld string    `json:"homeworld"`
	Films     []string  `json:"films"`
	Species   []string  `json:"species"`
	Vehicles  []string  `json:"vehicles"`
	Starships []string  `json:"starships"`
	Created   time.Time `json:"created"`
	Edited    time.Time `json:"edited"`
	URL       string    `json:"url"`
}

func main() {

}

This module I build with this command:

tinygo build -o module/http_client.wasm -target=wasip1 module/http_client/main.go

My tingo version is 0.33.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    wasmWebAssembly

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions