How to post a file and form fields in the same request?











up vote
0
down vote

favorite












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?










share|improve this question






















  • Where is api document?
    – KibGzr
    Nov 22 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 docs.bugsnag.com/api/js-source-map-upload
    – Markus Tenghamn
    Nov 23 at 9:38















up vote
0
down vote

favorite












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?










share|improve this question






















  • Where is api document?
    – KibGzr
    Nov 22 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 docs.bugsnag.com/api/js-source-map-upload
    – Markus Tenghamn
    Nov 23 at 9:38













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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?










share|improve this question













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?







go http-post






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 16:44









Markus Tenghamn

47321227




47321227












  • Where is api document?
    – KibGzr
    Nov 22 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 docs.bugsnag.com/api/js-source-map-upload
    – Markus Tenghamn
    Nov 23 at 9:38


















  • Where is api document?
    – KibGzr
    Nov 22 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 docs.bugsnag.com/api/js-source-map-upload
    – Markus Tenghamn
    Nov 23 at 9:38
















Where is api document?
– KibGzr
Nov 22 at 16:57




Where is api document?
– KibGzr
Nov 22 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 docs.bugsnag.com/api/js-source-map-upload
– Markus Tenghamn
Nov 23 at 9:38




@KibGzr I don't think it's directly relevant to the question but the Bugsnag documentation for uploading source maps can be found here docs.bugsnag.com/api/js-source-map-upload
– Markus Tenghamn
Nov 23 at 9:38












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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()





share|improve this answer





















  • This is the way to do it, thank you for the help
    – Markus Tenghamn
    Nov 23 at 10:07











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435274%2fhow-to-post-a-file-and-form-fields-in-the-same-request%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










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()





share|improve this answer





















  • This is the way to do it, thank you for the help
    – Markus Tenghamn
    Nov 23 at 10:07















up vote
1
down vote



accepted










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()





share|improve this answer





















  • This is the way to do it, thank you for the help
    – Markus Tenghamn
    Nov 23 at 10:07













up vote
1
down vote



accepted







up vote
1
down vote



accepted






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()





share|improve this answer












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()






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 17:03









KibGzr

1,424610




1,424610












  • This is the way to do it, thank you for the help
    – Markus Tenghamn
    Nov 23 at 10:07


















  • This is the way to do it, thank you for the help
    – Markus Tenghamn
    Nov 23 at 10:07
















This is the way to do it, thank you for the help
– Markus Tenghamn
Nov 23 at 10:07




This is the way to do it, thank you for the help
– Markus Tenghamn
Nov 23 at 10:07


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435274%2fhow-to-post-a-file-and-form-fields-in-the-same-request%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks