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/10/03 23:34:35 UTC

[GitHub] [pulsar] devinbost opened a new issue #5312: Function REST API documentation is insufficient for Create POST action

devinbost opened a new issue #5312: Function REST API documentation is insufficient for Create POST action
URL: https://github.com/apache/pulsar/issues/5312
 
 
   **Describe the bug**
   
   I’m attempting to create a function through the REST API, and I’m getting a 415 Unsupported Media Type response. I had this same issue when creating a PUT to create a new tenant through the REST API, but I resolved that issue by setting the content type to "application/json; charset=utf-8". However, with the POST of the function, I’m still getting this issue. 
   
   **To Reproduce**
   
   In the docs (https://pulsar.apache.org/functions-rest-api/?version=2.4.1#operation/registerFunction), the example of the expected POST body is this:
   ```
   {
     "inputs": persistent://public/default/input-topic,
     "parallelism": 4
     "output": persistent://public/default/output-topic
     "log-topic": persistent://public/default/log-topic
     "classname": org.example.test.ExclamationFunction
     "jar": java-function-1.0-SNAPSHOT.jar
   }
   ```
   
   I’ve checked the POST body that I'm sending via Wireshark, and the content is valid JSON.
   This is my post body:
   `{"namespace":"exampleNamespace","tenant":"exampleTenant","name":"exampleFunction","jar":"http://repository.domain.com/example-jar-1.0.1-20191001.001216-48-jar-with-dependencies.jar","className":"com.overstock.path.to.class","output":"persistent://exampleTenant/exampleNamespace/exampleOutputTopic","logTopic":"persistent://exampleTenant/exampleNamespace/exampleLogTopic","inputs":["persistent://exampleTenant/exampleNamespace/exampleInputTopic"],"userConfig":{"hostname":"endpoint.domain.com","port":"9082"}}`
   
   The method I'm using to send this JSON string is this (written in Golang):
   
   ```
   func createPost(serviceURL string, messageBody string) *http.Response {
       var netClient = &http.Client{
           Timeout: time.Second * 10,
       }
       log.Println("This is the body of our POST:")
       log.Println(messageBody)
       //msgBodyMarshalled, _ := json.Marshal(messageBody)
       msgBodyMarshalled := []byte(messageBody)
       resp, err := netClient.Post(serviceURL, "application/json; charset=utf-8", bytes.NewBuffer(msgBodyMarshalled))
       if err != nil {
           log.Fatalln(err)
       }
       defer resp.Body.Close()
   
       body, err := ioutil.ReadAll(resp.Body)
       if err != nil {
           log.Fatalln(err)
       }
       log.Println(string(body))
       return resp
   }
   ```
   
   In the source code for the endpoint, it’s not clear how the post body is translated to the endpoint because the expected parameters do not match the docs cited above (https://pulsar.apache.org/functions-rest-api/?version=2.4.1#operation/registerFunction):
   
   ```
   public FunctionsApiV3Resource() {
           this.functions = new FunctionsImpl(this);
       }
   
       @POST
       @Path("/{tenant}/{namespace}/{functionName}")
       @Consumes(MediaType.MULTIPART_FORM_DATA)
       public void registerFunction(final @PathParam("tenant") String tenant,
                                    final @PathParam("namespace") String namespace,
                                    final @PathParam("functionName") String functionName,
                                    final @FormDataParam("data") InputStream uploadedInputStream,
                                    final @FormDataParam("data") FormDataContentDisposition fileDetail,
                                    final @FormDataParam("url") String functionPkgUrl,
                                    final @FormDataParam("functionConfig") FunctionConfig functionConfig) {
   
           functions.registerFunction(tenant, namespace, functionName, uploadedInputStream, fileDetail,
                   functionPkgUrl, functionConfig, clientAppId(), clientAuthData());
       }
   ```
   
   From the Glassfish docs on @FormDataParam, it appears that the expected content type is actually `multipart/form-data`, not `application/json`. To investigate this, I created a Wireshark capture of a Functions Create message being sent from the Pulsar Admin CLI. Sure enough, the content of the message is `multipart/form-data`, not `application/json`. 
   
   **Expected behavior**
   The Pulsar docs should provide a clearer example of what is expected as the body for a POST for Functions Create. 
   
   **Screenshots**
   ![image](https://user-images.githubusercontent.com/7418031/66170969-b9eb3280-e603-11e9-860d-c52b7c4d9b57.png)
   
   

----------------------------------------------------------------
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