# 特定のルートで API が JSON の代わりに HTML を返す？

**URL:** https://meta.discourse.org/t/api-returning-html-instead-of-json-at-specific-routes/100691
**Category:** Development
**Created:** [2018 年 10 月 28 日午後 5:25 UTC](https://meta.discourse.org/t/api-returning-html-instead-of-json-at-specific-routes/100691 "2018-10-28T17:25:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Wintermute](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wintermute/32/127023_2.png) [@Wintermute](https://meta.discourse.org/u/Wintermute)
#### Post date: [2018 年 10 月 28 日午後 5:25 UTC](https://meta.discourse.org/t/api-returning-html-instead-of-json-at-specific-routes/100691/1 "2018-10-28T17:25:41Z")

</div>

> In Brief:

- calling a specific route returns HTML instead of json
- calling a different route with same code returns json as expected
- accessing the broken route from the Chrome browser returns json as expected

> Long:

I found an error with a URL with .json appended erroring with

```plaintext
FetchError: invalid json response body at //url// 
reason: Unexpected token < in JSON at position 0 

```

Did a little fiddling and found:

```plaintext
const request2 = 'https://{site-url}/u/{user}/activity.json?api_key={key}';

fetch(request2)
// .then(res => {console.log(res)}) // returns 200
.then(res => {return res.text()}) //.json was swapped for .text here
.then(res => {console.log(res)}) // returns html!

```

The output of the second console.log(), which we expect to be JSON, is HTML!

The odd thing:

Reusing the same code but a different route:

```plaintext
const request2 = 'https://{site-url}/u/{user}.json?api_key={key}'

```

returns json as expected. And even stranger, pasting the faulty URL into Chrome returns JSON as expected!

Any suggestions? Is this a discourse bug? Did I set something up wrong? I’m not the site admin but can request to get things fixed if it’s a site setup issue.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [2018 年 10 月 28 日午後 6:41 UTC](https://meta.discourse.org/t/api-returning-html-instead-of-json-at-specific-routes/100691/2 "2018-10-28T18:41:29Z")

</div>

> [@Wintermute](#):
>
> Any suggestions?

If you visit a user’s activity page on Discourse and view the Network tab of your browser’s developer tools, you’ll see that a `GET` request is made to a URL similar to:

```plaintext
http://forum.example.com/user_actions.json?username=simon

```

Querying that URL through the API should return JSON.

---

<div class="post-metadata">

### Author: ![Wintermute](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wintermute/32/127023_2.png) [@Wintermute](https://meta.discourse.org/u/Wintermute)
#### Post date: [2018 年 10 月 28 日午後 6:53 UTC](https://meta.discourse.org/t/api-returning-html-instead-of-json-at-specific-routes/100691/3 "2018-10-28T18:53:51Z")

</div>

Thanks.

`https://{site-url}/user_actions.json?username={user}&api_key={key}`

Does return the content that I need. However, shouldn’t the original path also work? You can see it being called in the network tab when accessing the user activity page and it’s got .json? appended to it.

---

<div class="post-metadata">

### Author: ![Wintermute](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wintermute/32/127023_2.png) [@Wintermute](https://meta.discourse.org/u/Wintermute)
#### Post date: [2018 年 11 月 21 日午後 10:08 UTC](https://meta.discourse.org/t/api-returning-html-instead-of-json-at-specific-routes/100691/4 "2018-11-21T22:08:27Z")

</div>

Continuing to face challenges with this issue in new situations

 ![UnexpectedToken](https://global.discourse-cdn.com/meta/original/3X/b/b/bb331a7da0af440f5ac41ceb1d0208c22638708d.png)

It seems in some instances the request is processed just fine and in some it fails and returns HTML.

Here I am sending individual requests for a large sequence of posts using the endpoint suggested at

> **[Discourse API Docs](https://docs.discourse.org/#tag/Posts%2Fpaths%2F~1posts~1%7Bid%7D%2Fget)**

Suggestions? I still believe there is something wonky going on on the backend that some of these routes would be failing.
