0

I have two services running as microservices, I get file from user using Service-1 and then try to POST it to Service-2, where it will be read and processed. Here what I had tried.

Service-1:

Form data is taken as:

<form  class="formContainer"  id="my_form" action="http://usagemeter:7171/addmanagerstouser">
</form>

Controller:

    adduserManagersReq := adminservice+"/addmanagerstouser"
    usagerManagersCsv := req.FormValue("user_managers")

    adduserManagersRes, err := http.PostForm(adduserManagersReq, url.Values{"usermanagers": {usagerManagersCsv}})

Here csv is passed as user_manager in html form.

Service-2:

func addManagersToUsers(res http.ResponseWriter,req *http.Request){
    file := req.FormValue("usermanagers")

    fd, err := os.Open(file)
    if err != nil {
        log.Println("Error opening file")
        fmt.Fprintf(res,"false")
        return
    }
    defer fd.Close()
}

Here I get No such file or directory.

What am I missing here? Is this possible? or have to use another way to do it?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Sachith Muhandiram
  • 2,819
  • 10
  • 45
  • 94
  • 2
    You are sending the file *name* from one service to the other. If the two services are running on different hosts, its not surprising the receiving service cannot open the file path. Did you mean to send the file body in the POST? – colm.anseo Aug 19 '20 at 15:50
  • @colm.anseo Yes, I want to send the content in that file, and read it from second service. – Sachith Muhandiram Aug 19 '20 at 15:52
  • Maybe https://www.grpc.io/ is something you want to look at :) – SamuelTJackson Aug 19 '20 at 17:17

1 Answers1

2

This answer shows host to POST a file's content:

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
}

And this shows how to receive it:

func ReceiveFile(w http.ResponseWriter, r *http.Request) {
    r.ParseMultipartForm(32 << 20) // limit your max input length!
    var buf bytes.Buffer
    file, header, err := r.FormFile("file")
    if err != nil {
        panic(err)
    }
    defer file.Close()
    name := strings.Split(header.Filename, ".")
    fmt.Printf("File name %s\n", name[0])
    io.Copy(&buf, file)
    contents := buf.String()
    fmt.Println(contents) // <- process file body here
    buf.Reset()

    return
}
colm.anseo
  • 19,337
  • 4
  • 43
  • 52