# Can't create GET request route

**URL:** https://meta.discourse.org/t/cant-create-get-request-route/71742
**Category:** Support
**Created:** [10.Октябрь.2017 08:44:48 UTC](https://meta.discourse.org/t/cant-create-get-request-route/71742 "2017-10-10T08:44:48Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Mathieu](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Mathieu](https://meta.discourse.org/u/Mathieu)
#### Post date: [10.Октябрь.2017 08:44:48 UTC](https://meta.discourse.org/t/cant-create-get-request-route/71742/1 "2017-10-10T08:44:48Z")

</div>

Hello,

I’m trying to create a custom API on Discourse for a plugin I’m working on.  
I haven’t any problem to create a `POST` request, but when I try to create one with `GET`, it all the time returns a 404 error page.

Here is my code.

```plaintext
after_initialize do
    module ::MyApiComments
        class Engine < ::Rails::Engine
            engine_name "myapi_comments"
            isolate_namespace MyApiComments
        end
    end
   
    class MyApiComments::CommentsController < ::ApplicationController
        def get_topic_comments
            # Some stuff here
            render :json => message, :status => 200
        end
    end

    MyApiComments::Engine.routes.draw do
        get '/myapi-t/:id' => 'comments#get_topic_comments'
    end

    Discourse::Application.routes.append do
        mount ::MyApiComments::Engine, at: "/"
    end
end

```

When I use `post '/myapi-t/:id' => 'comments#get_topic_comments'`, all works fine. But I absolutely need to use a GET request and I can’t understand why it doesn’t works.

(by the way, I know there is already an API route to get topic posts, but I don’t use it for good reasons :))

Thanks for your help :).

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [10.Октябрь.2017 08:54:13 UTC](https://meta.discourse.org/t/cant-create-get-request-route/71742/2 "2017-10-10T08:54:13Z")

</div>

If you check your Rails logs, which controller is that route hitting?

---

<div class="post-metadata">

### Author: ![Mathieu](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Mathieu](https://meta.discourse.org/u/Mathieu)
#### Post date: [10.Октябрь.2017 09:13:58 UTC](https://meta.discourse.org/t/cant-create-get-request-route/71742/3 "2017-10-10T09:13:58Z")

</div>

Thanks for your answer. Here is the log.

```plaintext
Processing by MyApiComment::CommentsController#get_topic_comments as */*
  Parameters: {"id"=>"8"}
  Rendered default/empty.html.erb within layouts/application (0.5ms)
  Rendered layouts/_head.html.erb (34.0ms)
  Rendered common/_google_universal_analytics.html.erb (0.8ms)
  Rendered common/_discourse_stylesheet.html.erb (21.6ms)
  Rendered common/_special_font_face.html.erb (2.3ms)
  Rendered application/_header.html.erb (0.8ms)
  Rendered common/_discourse_javascript.html.erb (117.0ms)
  Rendered common/_google_analytics.html.erb (0.7ms)
Completed 200 OK in 453ms (Views: 277.3ms | ActiveRecord: 47.3ms)

```

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [10.Октябрь.2017 10:33:56 UTC](https://meta.discourse.org/t/cant-create-get-request-route/71742/4 "2017-10-10T10:33:56Z")

</div>

Do you mean you’re getting `404` on the client side? Based on the logs you pasted, it is returning a `200` response.

---

<div class="post-metadata">

### Author: ![Mathieu](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Mathieu](https://meta.discourse.org/u/Mathieu)
#### Post date: [10.Октябрь.2017 10:50:32 UTC](https://meta.discourse.org/t/cant-create-get-request-route/71742/5 "2017-10-10T10:50:32Z")

</div>

Yeah, it seems it is a 200, not a 404 error page, sorry for mistake.  
What I’m really getting is an HTML view with header content (logo, nav, etc.), but not a json string with what I asked for (using `render :json`).

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [10.Октябрь.2017 13:07:07 UTC](https://meta.discourse.org/t/cant-create-get-request-route/71742/6 "2017-10-10T13:07:07Z")

</div>

This sounds like a an [http://www.perlmonks.org/?node=XY+Problem](http://www.perlmonks.org/?node=XY+Problem). You might get a better answer if you back up and describe what you are trying to accomplish.

---

<div class="post-metadata">

### Author: ![Mathieu](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Mathieu](https://meta.discourse.org/u/Mathieu)
#### Post date: [10.Октябрь.2017 13:20:12 UTC](https://meta.discourse.org/t/cant-create-get-request-route/71742/7 "2017-10-10T13:20:12Z")

</div>

A GET request which returns JSON, as I described on my first message :).  
Make a POST requests returns JSON I asked for, the GET one returns HTML. That is my current problem.

Thanks for trying to help me.

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [10.Октябрь.2017 14:54:14 UTC](https://meta.discourse.org/t/cant-create-get-request-route/71742/8 "2017-10-10T14:54:14Z")

</div>

When you request the route, try putting `.json` at the end.

When you request a page on discourse, by default it returns the Ember app. If you want to use it programmatically, you need to put `.json` at the end.

---

<div class="post-metadata">

### Author: ![Mathieu](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Mathieu](https://meta.discourse.org/u/Mathieu)
#### Post date: [10.Октябрь.2017 15:24:04 UTC](https://meta.discourse.org/t/cant-create-get-request-route/71742/9 "2017-10-10T15:24:04Z")

</div>

Ho.

Hmm, really nice thing to know :D.  
I knew (I’m using it for other things) that the Discourse API needs to end by `.json`, but on a specific route that I declared myself… Is this documented somewhere?

Really thanks for your answer.

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [08.Июнь.2024 12:44:08 UTC](https://meta.discourse.org/t/cant-create-get-request-route/71742/10 "2024-06-08T12:44:08Z")

</div>

Эта тема была автоматически закрыта через 2433 дня. Новые ответы больше не допускаются.
