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

Add a function for GET requests? #469

Open
Iskander0 opened this issue Mar 5, 2022 · 1 comment
Open

Add a function for GET requests? #469

Iskander0 opened this issue Mar 5, 2022 · 1 comment

Comments

@Iskander0
Copy link

Iskander0 commented Mar 5, 2022

Now you can do curl '$url', but it's faster to have a function for GETting built into ABS :

import "github.com/valyala/fasthttp"

// httpGET(string:"https://www.example.com")
func httpGETFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
	err := validateArgs(tok, "httpGET", args, 1, [][]string{{object.STRING_OBJ}})
	if err != nil { return err }

	url := args[0].(*object.String)

	readTimeout, _ := time.ParseDuration("1000ms")
	writeTimeout, _ := time.ParseDuration("1000ms")
	maxIdleConnDuration, _ := time.ParseDuration("1h")
	var client *fasthttp.Client
	client = &fasthttp.Client{
		ReadTimeout:                   readTimeout,
		WriteTimeout:                  writeTimeout,
		MaxIdleConnDuration:           maxIdleConnDuration,
		NoDefaultUserAgentHeader:      true, // Don't send: User-Agent: fasthttp
		DisableHeaderNamesNormalizing: true, // If you set the case on your headers correctly you can enable this
		DisablePathNormalizing:        true,
		// increase DNS cache time to an hour instead of default minute
		Dial: (&fasthttp.TCPDialer{ Concurrency:      4096, DNSCacheDuration: time.Hour, }).Dial,
	}

	req := fasthttp.AcquireRequest()
	req.SetRequestURI(url.Value)
	req.Header.SetMethod(fasthttp.MethodGet)
	resp := fasthttp.AcquireResponse()
	err1 := client.Do(req, resp)
	if err1 != nil { return &object.String{Token: tok, Value: err1.Error() } }
	fasthttp.ReleaseRequest(req)
	defer fasthttp.ReleaseResponse(resp)

	return &object.String{Token: tok, Value: string(resp.Body())}		
}

@odino
Copy link
Collaborator

odino commented Mar 7, 2022 via email

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

No branches or pull requests

2 participants