reqhtml

package
v0.24.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package reqhtml contains utilities for sending and receiving x/net/html objects.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Body

func Body(n *html.Node) requests.Config

Body sets the requests.Builder's request body to the HTML document. It also sets ContentType to "text/html" if it is not otherwise set.

Example
package main

import (
	"context"
	"fmt"
	"net/http/httputil"

	"github.com/carlmjohnson/requests"
	"github.com/carlmjohnson/requests/reqhtml"
	"golang.org/x/net/html"
)

func main() {
	link := html.Node{
		Type: html.ElementNode,
		Data: "a",
		Attr: []html.Attribute{
			{Key: "href", Val: "http://example.com"},
		},
	}
	text := html.Node{
		Type: html.TextNode,
		Data: "Hello, World!",
	}
	link.AppendChild(&text)

	req, err := requests.
		URL("http://example.com").
		Config(reqhtml.Body(&link)).
		Request(context.Background())
	if err != nil {
		panic(err)
	}
	b, err := httputil.DumpRequest(req, true)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%q\n", b)

}
Output:

"POST / HTTP/1.1\r\nHost: example.com\r\nContext-Type: text/html\r\n\r\n<a href=\"http://example.com\">Hello, World!</a>"

func To

To decodes a response as an html document.

Example
package main

import (
	"context"
	"fmt"

	"github.com/carlmjohnson/requests"
	"github.com/carlmjohnson/requests/reqhtml"
	"golang.org/x/net/html"
	"golang.org/x/net/html/atom"
)

func main() {
	var doc html.Node
	err := requests.
		URL("http://example.com").
		Handle(reqhtml.To(&doc)).
		Fetch(context.Background())
	if err != nil {
		fmt.Println("could not connect to example.com:", err)
	}
	var f func(*html.Node)
	f = func(n *html.Node) {
		if n.DataAtom == atom.A {
			for _, attr := range n.Attr {
				if attr.Key == "href" {
					fmt.Println("link:", attr.Val)
				}
			}
		}
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			f(c)
		}
	}
	f(&doc)
}
Output:

link: https://www.iana.org/domains/example

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL