Пытаюсь отправить POST запрос на сервер, но при попытке чтения ответа (ответ в формате JSON) от сервера выдает ошибку -
spoilerpanic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x640afd]
goroutine 1 [running]:
main.newfileUploadRequest(0x6c8caf, 0x13, 0x6c54f8, 0x5, 0x6cf980, 0x26, 0x0, 0x0, 0x0)
/home/q/Desktop/requests/req.go:36 +0x29d
main.main()
/home/q/Desktop/requests/req.go:45 +0x72
exit status 2
package main
import (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {
url := "localhost:6000/..."
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/home/...")
defer file.Close()
part1, errFile1 := writer.CreateFormFile("Image",filepath.Base("/home/..."))
_, errFile1 = io.Copy(part1, file)
if errFile1 !=nil {
fmt.Println(errFile1)
}
err := writer.Close()
if err != nil {
fmt.Println(err)
}
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}