Adding a REST route endpoint

In my MyPluginModule.java I add addResource(MyPluginResource.class);

My class looks like this

@Api(value = "Logo", description = "Upload New Logo")
@Path("/uploadLogo")
public class MyPluginResource implements PluginRestResource {

    @PUT
    @Timed
    @ApiOperation(value = "Upload Logo")
    @Consumes(MediaType.APPLICATION_JSON)
    public void String request) {
       //process JSON request string here
    }
}

From my React frontend I create a ajax method with the route /plugins/org.graylog.plugins/uploadLogo

What’s your question?

How do I properly set up a REST endpoint for passing data back!? I followed the example in the Map plugin on the graylog github. I thought I had everything setup correctly (see first post). But when I try to do a PUT it says that the path does not exist. So I must be missing something.

You’re not using the correct URI to access your JAX-RS resource.

The plugin resources are prefixed by the fully qualified package name of the class implementing Plugin, e. g. org.graylog.plugins.map in case of the Map Widget plugin.

I ensured that my URI is referencing my package name. But it still does not work.

My folder structure is this

I ensured that my URI in the index.js is pointing to /plugins/org.graylog.plugins.logo/uploadLogo

Please share the relevant code of your Java and JavaScript components.

LogoReplacerConfig.jsx

  $.ajax({
      type: 'POST',
      url: '/plugins/org.graylog.plugins.logo/uploadLogo',
      data: this.state.config,
      processData: false,
      contentType: false
    })
    .done(function(data) {
      console.log('success');
      this._closeModal();
    });

LogoReplacerResource.java

package org.graylog.plugins.logo.rest;
@RequiresAuthentication
@Api(value = "Logo", description = "Upload New Logo")
@Path("/uploadLogo")
public class LogoReplacerResource implements PluginRestResource {
    private static final Logger LOG = LoggerFactory.getLogger(LogoReplacerResource.class);

    @POST
    @Timed
    @ApiOperation(value = "Upload Logo")
    @Consumes(MediaType.APPLICATION_JSON)
    @NoAuditEvent("only used to uplooad a new image to graylog")
    public void upload(@ApiParam(name = "image", required = true) String request) {
    }
}

LogoReplacerModule.java

package org.graylog.plugins.logo;
import org.graylog2.plugin.PluginConfigBean;
import org.graylog2.plugin.PluginModule;
import org.graylog.plugins.logo.rest.LogoReplacerResource;
public class LogoReplacerModule extends PluginModule {
    private static final Logger LOG = LoggerFactory.getLogger(LogoReplacerModule.class);
  
    @Override
    public Set<? extends PluginConfigBean> getConfigBeans() {
        return Collections.emptySet();
    }

    @Override
    protected void configure() {
      addRestResource(LogoReplacerResource.class);
    }
}

You’re not sending any authentication information to the resource but annotated it with @RequiresAuthentication.
Additionally, you’re not sending any content type which isn’t strictly required but recommended.

Adding a content type and removing @RequiresAuthentication still resolves in a POST http://localhost:8080/plugins/org.graylog.plugins.logo/uploadLogo 404 (Not Found)

$.ajax({
      type: 'POST',
      url: '/plugins/org.graylog.plugins.logo/uploadLogo',
      data: 'text',
      contentType: 'plain/text'
    })
    .done(function(data) {
      console.log('success');
      this._closeModal();
    });

Did you properly register your resource?
Did you try using the correct content type? It says application/json in the JAX-RS resource but plain/text (did you mean text/plain?) in the JavaScript code.

Anyway, I will stop guessing here without having access to the full code.

I have registered my resource in the module by using addRestResource(LogoReplacerResource.class). Here is a link to my source code graylog-plugin-logo-replacer

When building the plugin from the GitHub repository you’ve posted and installing it in Graylog, I get the following output:

$ curl -u admin:admin -i -X POST -H 'Content-Type: application/json' -d '{}' http://127.0.0.1:9000/api/plugins/org.graylog.plugins.logo/uploadLogo
HTTP/1.1 204 No Content
X-Graylog-Node-ID: cd03ee44-b2a7-4824-be16-bb7456149dbd
X-Runtime-Microseconds: 724
Date: Fri, 28 Jul 2017 11:49:20 GMT

This means that the resource has been properly registered and is accessible through the Graylog REST API. It’s also shown in the API browser.

Awesome!!! So I was using the wrong URL to post to in my frontend component. I was hitting the standard web interface (port 8080) and not the API route (port 9000). Thanks so much for your help!

Also from my react component I needed to to a fetch call instead of a jQuery ajax call.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.