Retirer JSON de form création de document
Utiliser context#Render pour afficher une string Message à l'écran.
This commit is contained in:
parent
cc4ab5851d
commit
5d984ccacb
2 changed files with 29 additions and 23 deletions
16
api/api.go
16
api/api.go
|
@ -109,7 +109,7 @@ func (a *API) Call(method, route string) ([]byte, error) {
|
|||
return nil, errors.New(fmt.Sprintf("method must be 'GET' or 'DELETE', got '%s'", method))
|
||||
}
|
||||
|
||||
func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (string, error) {
|
||||
func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (map[string]interface{}, error) {
|
||||
endpoint := fmt.Sprintf("%s://%s:%d",
|
||||
a.Protocol,
|
||||
a.Host,
|
||||
|
@ -125,29 +125,29 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (
|
|||
// Add the file to the request
|
||||
file, err := file_header.Open()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("UploadDocument#file_header.Open: %s", err)
|
||||
return nil, fmt.Errorf("UploadDocument#file_header.Open: %s", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
part, err := writer.CreateFormFile("document", file_header.Filename)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err)
|
||||
return nil, fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err)
|
||||
}
|
||||
|
||||
_, err = io.Copy(part, file)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("UploadDocument#io.Copy: %s", err)
|
||||
return nil, fmt.Errorf("UploadDocument#io.Copy: %s", err)
|
||||
}
|
||||
|
||||
err = writer.Close()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("UploadDocument#writer.Close: %s", err)
|
||||
return nil, fmt.Errorf("UploadDocument#writer.Close: %s", err)
|
||||
}
|
||||
|
||||
// Create a new HTTP request with the multipart body
|
||||
req, err := http.NewRequest(http.MethodPost, url, body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("UploadDocument#http.NewRequest: %s", err)
|
||||
return nil, fmt.Errorf("UploadDocument#http.NewRequest: %s", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
|
@ -160,7 +160,7 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (
|
|||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("UploadDocument#client.Do: %s", err)
|
||||
return nil, fmt.Errorf("UploadDocument#client.Do: %s", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
|
@ -168,7 +168,7 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (
|
|||
var res map[string]interface{}
|
||||
|
||||
json.NewDecoder(resp.Body).Decode(&res)
|
||||
return fmt.Sprintf("%s", res), nil
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// CallWithData takes data and returns a string representing a response body.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue