logo
Tags down

shadow

http PUT request to upload zip file in golang


By : user2175269
Date : October 15 2020, 08:10 PM
hope this fix your issue This is a working example.
Most likely, it comes down to the fact that the server is a bit simple and takes the filename literally. And since you are not using filepath.Base on your uploadFileName, it might have path elements on it. Simply use it on your filename for testing purposes.
code :
package main

import (
    "bytes"
    "flag"
    "fmt"
    "io"
    "log"
    "mime/multipart"
    "net"
    "net/http"
    "os"
    "path/filepath"
    "sync"
)

var (
    targetPath string
    filename   string
    port       int
    handlerLog *log.Logger
    mainLog    *log.Logger
)

// This is automatically called after vars have been initialized and before main
func init() {
    flag.StringVar(&targetPath, "target", "./tmp", "target directory for uploads")
    flag.StringVar(&filename, "file", "", "file to upload")
    flag.IntVar(&port, "port", 0, "port to listen on. When 0, a random port is assigned")
    handlerLog = log.New(os.Stdout, "[handler] ", log.LstdFlags)
    mainLog = log.New(os.Stdout, "[main   ] ", log.LstdFlags)
}

// By returning a handler, we have an elegant way of initializing path.
func uploadHandler(path string) http.Handler {

    // We make sure path is an existing directory when the handler takes over
    if s, err := os.Stat(path); err != nil {
        if os.IsNotExist(err) {
            handlerLog.Printf("Target '%s' does not exist. Creating it...", path)
            if cerr := os.MkdirAll(path, 0755); cerr != nil {
                handlerLog.Fatalf("Creating target: %s", err)
            }
        } else {
            handlerLog.Fatalf("Error accessing '%s': %s", path, err)
        }
    } else if !s.IsDir() {
        handlerLog.Fatalf("Target '%s' is not a directory", path)
    }

    // Do NOT use this handler in production!!!
    // It is lacking all sorts of safety measures.
    // However, it is enough to demonstrate.
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        handlerLog.Println("Handling file upload...")

        handlerLog.Println("Parsing form...")
        if err := r.ParseMultipartForm(32 << 20); err != nil {
            handlerLog.Fatalf("Parsing form: %s", err)
        }
        f, h, err := r.FormFile("file")
        if err != nil {
            handlerLog.Printf("Error accessing file: %s", err)
            http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
            return
        }
        defer f.Close()

        handlerLog.Println("Opening output file...")
        t, err := os.OpenFile(filepath.Join(path, filepath.Base(h.Filename)), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
        if err != nil {
            handlerLog.Printf("Opening output file: %s", err)
            http.Error(w, http.StatusText(http.StatusInternalServerError)+": "+err.Error(), http.StatusInternalServerError)
            return
        }
        defer t.Close()

        handlerLog.Println("Copying to output file...")
        if _, err = io.Copy(t, f); err != nil {
            handlerLog.Printf("Copying to output file: %s", err)

            http.Error(w, http.StatusText(http.StatusInternalServerError)+": "+err.Error(), http.StatusInternalServerError)
            return
        }
        handlerLog.Println("Finished handler!")
    })
}

func main() {
    flag.Parse()

    // Check input
    if filename == "" {
        mainLog.Fatal("No filename given. Exiting...")
    }

    mainLog.Println("Setting up upload handler...")
    http.Handle("/upload", uploadHandler(targetPath))

    wg := sync.WaitGroup{}
    wg.Add(1)

    // We want to finish the program after upload, as we only want to demonstrate
    go func() {
        mainLog.Println("Setting up listener...")

        listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
        if err != nil {
            mainLog.Fatalf("%s", err)
        }
        defer listener.Close()

        port = listener.Addr().(*net.TCPAddr).Port
        mainLog.Printf("Listening to port %d on localhost", port)

        wg.Done()
        http.Serve(listener, nil)
    }()

    buf := bytes.NewBuffer(nil)
    bodyWriter := multipart.NewWriter(buf)

    // We need to truncate the input filename, as the server might be stupid and take the input
    // filename verbatim. Then, he will have directory parts which do not exist on the server.
    fileWriter, err := bodyWriter.CreateFormFile("file", filepath.Base(filename))
    if err != nil {
        mainLog.Fatalf("Creating fileWriter: %s", err)
    }

    file, err := os.Open(filename)
    if err != nil {
        mainLog.Fatalf("Opening file: %s", err)
    }
    defer file.Close()

    if _, err := io.Copy(fileWriter, file); err != nil {
        mainLog.Fatalf("Buffering file: %s", err)
    }

    // We have all the data written to the bodyWriter.
    // Now we can infer the content type
    contentType := bodyWriter.FormDataContentType()

    // This is mandatory as it flushes the buffer.
    bodyWriter.Close()

    // As we wait for the server to spin up, we need to wait here.
    mainLog.Println("Waiting for the listener to be set up...")
    wg.Wait()

    req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("http://127.0.0.1:%d/upload", port), buf)
    if err != nil {
        mainLog.Fatalf("Creating request: %s", err)
    }
    req.Header.Set("Content-Type", contentType)

    client := http.Client{}

    mainLog.Println("Sending file")
    res, err := client.Do(req)
    if err != nil {
        mainLog.Fatalf("Sending file: %s", err)
    }

    mainLog.Printf("Received %s from server. Exiting...", res.Status)
}


Share : facebook icon twitter icon

Golang upload Http request FormFile to Amazon S3


By : r mac
Date : March 29 2020, 07:55 AM
This might help you I'm creating a micro service to handle some attachments uploads to Amazon S3, What I'm trying to achieve is accept a file and then store it directly to my Amazon S3 bucket, my current function : , Use ioutil.ReadAll:
code :
bs, err := ioutil.ReadAll(file)
// ...
err = bucket.Put(
    header.Filename, 
    bs, 
    header.Header.Get("Content-Type"), 
    s3.ACL("public-read"),
)
bucket.PutReader(
    header.Filename, 
    file, 
    fileSize, 
    header.Header.Get("Content-Type"), 
    s3.ACL("public-read"),
)

GoLang: Setting header to null for a file:// to http:// request not working


By : aditya
Date : March 29 2020, 07:55 AM
I wish this helpful for you After writing up this question, I thought to try one last thing out of desperation, and it worked.
code :
func handler(w http.ResponseWriter, r *http.Request) {
    w.Header().Add("Access-Control-Allow-Origin", "null")
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

How to let Jmeter Http request sampler preload and resue the upload file in its upload test?


By : Ian Hutchinson
Date : March 29 2020, 07:55 AM
this one helps. Actually, you can preload the file using BeanShell sampler and store its value in single variable once. But the problem is you can't share variables between threads. So, once read and stored as variable, it can be only used in the same thread.
To share parameters between threads, it's possible to use properties, but not sure that you can do the same with files as they contain binaries.

How to convert this curl request to Http post request for file upload in java?


By : Ram Krix
Date : March 29 2020, 07:55 AM
will help you You specify content-type application/x-www-form-urlencoded (as curl does for this case) but supply an actual body (entity) that corresponds to multipart/form-data which is radically different. Instead use URLEncodedFormEntity containing (for your case) one NameValuePair something like this:
code :
byte[] contents = Files.readAllBytes (new File(filepath).toPath());
List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("data1", new String(contents,charset));
uploadFile.setEntity(new UrlEncodedFormEntity (list));

Upload a file with POST request golang


By : J.Doe
Date : March 29 2020, 07:55 AM
this will help I'm new to golang and I'm trying to write a function that uploads a file with a post request to telegram for a bot I'm writing. , After some digging I figured it out with this
code :
func SendPostRequest (url string, filename string, filetype string) []byte {
    file, err := os.Open(filename)

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


    body := &bytes.Buffer{}
    writer := multipart.NewWriter(body)
    part, err := writer.CreateFormFile(filetype, filepath.Base(file.Name()))

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

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

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

    request.Header.Add("Content-Type", writer.FormDataContentType())
    client := &http.Client{}

    response, err := client.Do(request)

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

    content, err := ioutil.ReadAll(response.Body)

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

    return content
}
shadow
Privacy Policy - Terms - Contact Us © voile276.org