Files
cariddi/crawler/requests.go
T
2021-04-28 21:54:15 +02:00

62 lines
1.2 KiB
Go

package crawler
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
//GetRequest
func GetRequest(target string) (string, error) {
resp, err := http.Get(target)
if err != nil {
return "", err
}
defer resp.Body.Close()
//We Read the response body on the line below.
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
//Convert the body to type string
sb := string(body)
return sb, nil
}
//PostRequest
func PostRequest(target string, data map[string]string) (string, error) {
postBody, _ := json.Marshal(data)
responseBody := bytes.NewBuffer(postBody)
//Leverage Go's HTTP Post function to make request
resp, err := http.Post(target, "application/json", responseBody)
//Handle Error
if err != nil {
return "", err
}
defer resp.Body.Close()
//Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
sb := string(body)
return sb, nil
}
//HeadRequest
func HeadRequest(target string) (string, error) {
resp, err := http.Head(target)
if err != nil {
return "", err
}
defer resp.Body.Close()
//Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
sb := string(body)
return sb, nil
}