You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2019/11/08 11:49:10 UTC

[GitHub] [pulsar] wolfstudy commented on issue #5312: Function REST API documentation is insufficient for Create POST action

wolfstudy commented on issue #5312: Function REST API documentation is insufficient for Create POST action
URL: https://github.com/apache/pulsar/issues/5312#issuecomment-551653091
 
 
   @devinbost Sorry, I missed this message.
   
   The REST API for create function should be correct. In [pulsarctl](https://github.com/streamnative/pulsarctl/blob/master/pkg/pulsar/functions.go#L142), I also called this REST API and successfully created the function.
   
   Copy the code from pulsarctl:
   
   ```
   func (f *functions) createStringFromField(w *multipart.Writer, value string) (io.Writer, error) {
   	h := make(textproto.MIMEHeader)
   	h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s" `, value))
   	h.Set("Content-Type", "application/json")
   	return w.CreatePart(h)
   }
   
   func (f *functions) CreateFunc(funcConf *utils.FunctionConfig, fileName string) error {
   	endpoint := f.client.endpoint(f.basePath, funcConf.Tenant, funcConf.Namespace, funcConf.Name)
   
   	// buffer to store our request as bytes
   	bodyBuf := bytes.NewBufferString("")
   
   	multiPartWriter := multipart.NewWriter(bodyBuf)
   
   	jsonData, err := json.Marshal(funcConf)
   	if err != nil {
   		return err
   	}
   
   	stringWriter, err := f.createStringFromField(multiPartWriter, "functionConfig")
   	if err != nil {
   		return err
   	}
   
   	_, err = stringWriter.Write(jsonData)
   	if err != nil {
   		return err
   	}
   
   	if fileName != "" && !strings.HasPrefix(fileName, "builtin://") {
   		// If the function code is built in, we don't need to submit here
   		file, err := os.Open(fileName)
   		if err != nil {
   			return err
   		}
   		defer file.Close()
   
   		part, err := multiPartWriter.CreateFormFile("data", filepath.Base(file.Name()))
   
   		if err != nil {
   			return err
   		}
   
   		// copy the actual file content to the filed's writer
   		_, err = io.Copy(part, file)
   		if err != nil {
   			return err
   		}
   	}
   
   	// In here, we completed adding the file and the fields, let's close the multipart writer
   	// So it writes the ending boundary
   	if err = multiPartWriter.Close(); err != nil {
   		return err
   	}
   
   	contentType := multiPartWriter.FormDataContentType()
   	err = f.client.postWithMultiPart(endpoint, nil, bodyBuf, contentType)
   	if err != nil {
   		return err
   	}
   
   	return nil
   }
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services