Skip to content

http.ServeContent Vs http.ServeFile #7

@mrodriguez3313

Description

@mrodriguez3313

I run into this error: http: superfluous response.WriteHeader call from main.writeJSONError (server.go:218) When attempting to set the header to StatusInternalServerError AFTER I have already sent the file using http.ServeContent:
`}

w.Header().Set("Content-Disposition", "attachment; filename="+string(keyName))
w.Header().Set("Content-Type", "application/octet-stream")
http.ServeFile(w, r, string(keyName) + ".key") // serve key to user to download

err = diskDelete(keyName) // delete key from disk
if err != nil {
	panic(err)
	return
}

err = diskDelete(keyName) // delete key from disk again to force error
if err != nil {
	writeJSONError(w, "Error in deleteKey", err) // Sets w.WriteHeader to 500
	return
}

`

This is annoying because I want to make sure the key is deleted from disk properly before I send the file to user. The obvious problem is that If I delete the file before I send it, then I don't have a file to send. I just learned of the function ServeContent. And maybe there is someway to save the file into a buffer and send that to user. Not exactly sure if that is exactly the function I need though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DiscussionDiscuss possible optionsbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions