0

I have the following code which I have tried to use to send a post request where I want to upload a js.map file to bugsnag.

...  
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(filetype, filename)

if err != nil {
    log.Fatal(err)
}

fmt.Printf("Sending %s to bugsnag", filename)

io.Copy(part, file)
writer.Close()
request, err := http.NewRequest("POST", bugsnagUrl, body)

if err != nil {
    log.Fatal(err)
}

request.Header.Add("Content-Type", writer.FormDataContentType())

// This is where my problem seems to be, I can't add form values this way because the map is nil
request.Form.Add("apiKey", bugsnagToken)
request.Form.Add("minifiedUrl", fileurl)
request.Form.Add("sourceMap", filename)
request.Form.Add("overwrite", "true")
client := &http.Client{}

response, err := client.Do(request)

if err != nil {
    log.Fatal(err)
}
defer response.Body.Close()

The above example gives the following error

panic: assignment to entry in nil map

goroutine 1 [running]: net/url.url.Values.Add(...)

I have been unable to find any examples on how to do this, I used the example on how to upload a file from this question to get this far.

I seem to be able to post the file but how can I post a file and also include post form values with my request?

Uberswe
  • 1,038
  • 2
  • 16
  • 36
  • Where is api document? – KibGzr Nov 22 '18 at 16:57
  • @KibGzr I don't think it's directly relevant to the question but the Bugsnag documentation for uploading source maps can be found here https://docs.bugsnag.com/api/js-source-map-upload/ – Uberswe Nov 23 '18 at 09:38

1 Answers1

1

You should add form fields like this

writer.WriteField("apiKey", bugsnagToken)
writer.WriteField("minifiedUrl", fileurl)
writer.WriteField("sourceMap", filename)
writer.WriteField("overwrite", "true")
writer.Close()
KibGzr
  • 2,053
  • 14
  • 15