Skip to content

Instantly share code, notes, and snippets.

@radhack
Created December 6, 2018 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radhack/fa5e85e93f807f24d8fd7f4205798c91 to your computer and use it in GitHub Desktop.
Save radhack/fa5e85e93f807f24d8fd7f4205798c91 to your computer and use it in GitHub Desktop.
Another Go NonEmbedded Signature Request
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
)
// Creates a new file upload http request with optional extra params
func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
fileContents, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
fi, err := file.Stat()
if err != nil {
return nil, err
}
file.Close()
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, fi.Name())
if err != nil {
return nil, err
}
part.Write(fileContents)
for key, val := range params {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return nil, err
}
return http.NewRequest("POST", uri, body)
}
func main() {
path, _ := os.Getwd()
path += "/10.docx"
extraParams := map[string]string{
"title": "My Document",
"message": "message here",
"signer[0][name]": "alex",
"signer[0][email_address]": "alex@hellosign.com",
"test_mode": "true",
}
request, err := newfileUploadRequest("https://APIKEYHERE:@api.hellosign.com/v3/signature_request/send", extraParams, "file", "10.docx")
if err != nil {
log.Fatal(err)
}
client := &http.Client{}
resp, err := client.Do(request)
defer resp.Body.Close()
if err != nil {
log.Fatal(err)
} else {
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(resp)
fmt.Println(string(body))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment