How to retrieve content of file uploaded using "curl --upload-file ..." way in Go HttpServer

1,593 views
Skip to first unread message

Budh Ram

unread,
Oct 7, 2014, 9:56:09 AM10/7/14
to golan...@googlegroups.com
Hi Guys,

I am newbie in Go language.
I am writing a small HttpServer in Go where I want to upload file to this server using curl approach as:
curl --upload-file foo.txt http://localhost:9090/rp.txt

I am getting following server logs: https://gist.github.com/budhrg/355126ba4f03d32008e0

I searched through docs, existing codes but the closest I found is file upload using web.

Following is my code for HttpServe which I need to update to accept file content and save in server:
package main

import (
  "fmt"
  "net/http"
)

func main() {
  http.Handle("/", http.HandlerFunc(upload))
  http.ListenAndServe("localhost:9090", nil)
}

func upload(w http.ResponseWriter, req *http.Request) {
  fmt.Fprintf(w, "file name is : %s",req.URL.Path[1:])
  fmt.Println("method :", req.Method)
  fmt.Println("FileSize : ", req.ContentLength)
  fmt.Println("Header : ",req.Header)
  fmt.Println("Body : ",req.Body)
  fmt.Println("Post form : ",req.PostForm)
}


Please help
 
Message has been deleted

Tobias S.

unread,
Oct 7, 2014, 1:13:10 PM10/7/14
to golan...@googlegroups.com
Hi Budh,
Maybe do it like this:
func copyFileHandler(w http.ResponseWriter, r *http.Request) {
app := "scp"
cmd := exec.Command(app, EPGMaker.PathToXml, PathToServer)
_, err := cmd.Output()
if err != nil {
println("ERROR:", err.Error())
return
}

if msg, ok := err.(*exec.ExitError); ok { // there is error code
os.Exit(msg.Sys().(syscall.WaitStatus).ExitStatus())
} else {
//os.Exit(0)


erfolgTempl.Execute(w, r.Host)
fmt.Println("HELLO")
}
}

The variable app holds the BASH command you would like to execute, in your case curl. To handle BASH return code you have to use a sophisticated syscall. 
You could put the file name to upload into a different command variable. 
Hope it helps...

Tobias S.

unread,
Oct 7, 2014, 4:44:13 PM10/7/14
to golan...@googlegroups.com
Sorry, I misunderstood your problem, so my code snippet is probably useless for you. But I found this:
The code sample can be fixed easily and should be what you are after.  I guess it is also better to adapt then the example you cited. 

Entefunc webUploadHandler(w http.ResponseWriter, r *http.Request) {

 file, header, err := r.FormFile("file") // the FormFile function takes in the POST input id file

 if err != nil {
    fmt.Fprintln(w, err)
    return
 }
 defer file.Close()

 // My error comes here

 messageId := r.URL.Query()["id"][0]
 out, err := os.Create("./upload/" + messageId + ".mp3")

 if err != nil {
    fmt.Fprintf(w, "Unable to create the file for writing. Check your write access privilege")
    return
 }
 defer out.Close()

 // write the content from POST to the file
 _, err = io.Copy(out, file)
 if err != nil {
    fmt.Fprintln(w, err)
 }

 fmt.Fprintf(w,"File uploaded successfully : ")
 fmt.Fprintf(w, header.Filename)

}r code here...



On Tuesday, October 7, 2014 3:56:09 PM UTC+2, Budh Ram wrote:

Budh Ram

unread,
Oct 8, 2014, 4:14:45 AM10/8/14
to golan...@googlegroups.com
Thanks Tobias for reply.

But instead of uploading file from form request, I want to upload it using curl --upload-file syntax as:
curl --upload-file foo.txt http://localhost:9090/rp.txt

When I use "file, header, err := r.FormFile("file")", I get error as:
request Content-Type isn't multipart/form-data 

on above mentioned curl command.

Michael Hofmann

unread,
Oct 8, 2014, 4:44:59 AM10/8/14
to golan...@googlegroups.com
Conventional, browser-based uploads use POST requests, where the request
body contains the content of all form fields (including files) in MIME
multipart encoding.
IIRC, curl can use this upload method as well. With --upload-file
however, it sends a PUT request with the contents of the single file
specified on the command line as body. In the simplest case, something
like "io.Copy(file, req.Body)" should do what you want.

On 08.10.2014 10:14, Budh Ram wrote:
> Thanks Tobias for reply.
>
> But instead of uploading file from form request, I want to upload it
> using curl --upload-file syntax as:
>
> curl --upload-file foo.txt http://localhost:9090/rp.txt
>
>
> When I use "file, header, err := r.FormFile("file")", I get error as:
>
> request Content-Type isn't multipart/form-data
>
>
> on above mentioned curl command.
>
> On Tuesday, 7 October 2014 19:26:09 UTC+5:30, Budh Ram wrote:
>
> Hi Guys,
>
> I am newbie in Go language.
> I am writing a small HttpServer in Go where I want to upload file to
> this server using curl approach as:
>
> curl --upload-file foo.txt http://localhost:9090/rp.txt
>
>
> I am getting following server logs:
> https://gist.github.com/budhrg/355126ba4f03d32008e0
> <https://gist.github.com/budhrg/355126ba4f03d32008e0>
>
> I searched through docs, existing codes but the closest I found is
> file upload using web
> <https://github.com/Blacktremolo/goload/blob/master/main.go#L49>.
>
> Following is my code for HttpServe which I need to update to accept
> file content and save in server:
>
> package main
>
>
> import (
>
> "fmt"
>
> "net/http"
>
> )
>
>
> func main() {
>
> http.Handle("/", http.HandlerFunc(upload))
>
> http.ListenAndServe("localhost:9090", nil)
>
> }
>
>
> func upload(w http.ResponseWriter, req *http.Request) {
>
> fmt.Fprintf(w, "file name is : %s",req.URL.Path[1:])
>
> fmt.Println("method :", req.Method)
>
> fmt.Println("FileSize : ", req.ContentLength)
>
> fmt.Println("Header : ",req.Header)
>
> fmt.Println("Body : ",req.Body)
>
> fmt.Println("Post form : ",req.PostForm)
>
> }
>
>
>
> Please help
>
> --
> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to golang-nuts...@googlegroups.com
> <mailto:golang-nuts...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

Budh Ram

unread,
Oct 8, 2014, 6:03:40 AM10/8/14
to golan...@googlegroups.com
Hi Michael,

Thanks. It is working now. I was assuming the simple case to complex one :) Thanks anyway.

Kannan S

unread,
Aug 16, 2021, 12:11:56 PM8/16/21
to golang-nuts
messageId := r.URL.Query()["id"][0]
 out, err := os.Create("./upload/" + messageId + ".mp3")

 if err != nil {
    fmt.Fprintf(w, "Unable to create the file for writing. Check your write access privilege")
    return
 }

i added this portion but i am getting an error while trying to upload a file using curl
 error in server is :panic serving 10.10.20.55:62253: runtime error: index out of range [0] with length 0
what is the reason??

Roland Müller

unread,
Aug 17, 2021, 1:10:50 AM8/17/21
to golang-nuts
Hello,


Am Mo., 16. Aug. 2021 um 19:11 Uhr schrieb Kannan S <kannan...@gmail.com>:
messageId := r.URL.Query()["id"][0]
 out, err := os.Create("./upload/" + messageId + ".mp3")

in your original example file name is retrieved from the request.URL.Path rather than from an variable named 'id' using request.URL.Query().

This should work when using the curl command below (haven't tried it out). The line replaces the two lines setting messageId and using message Id for creating the upload file.

out,err = os.Create("./upload/" + r.URL.Path[1:])

BR,
Roland

 
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/85f0c138-86e0-4d31-8c1b-4c6ef1d161a6n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages