# Content type of links

**URL:** <https://discuss.jsonapi.org/t/content-type-of-links/1553>\
**Category:** Uncategorized\
**Created:** [March 23, 2019, 2:45pm UTC](https://discuss.jsonapi.org/t/content-type-of-links/1553 "2019-03-23T14:45:42Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bedweight](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@bedweight](https://discuss.jsonapi.org/u/bedweight)\
**Post date:** [March 23, 2019, 2:45pm UTC](https://discuss.jsonapi.org/t/content-type-of-links/1553/1 "2019-03-23T14:45:42Z")

</div>

Are there any recommended strategies for specifying the content type of links? E.g. suppose that in addition to the JSON representation of the resource pointed to by `links.self`, there are HTML and PDF versions of the resource.

In general you’d want your JSON, HTML and PDF to all reside at the same location (`self` link), and for the requested Content-Type to be discriminating factor, but

- a) how do you document that the alternative formats are available if you only include a single `self`, and
- b) what if the other representations don’t actually reside at the same address?

```auto
{
  ...
  "id": 1,
  "links": {
    "self": "https://api.com/resources/1",
    "pdf": "https://api.com/resources/obfuscated-pdf-url",
    ...
  }
}

```

^ Is all I can think of.

---

<div class="post-metadata">

**Author:** ![fdrake](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/fdrake/32/654_2.png) [@fdrake](https://discuss.jsonapi.org/u/fdrake)\
**Post date:** [December 18, 2019, 6:39pm UTC](https://discuss.jsonapi.org/t/content-type-of-links/1553/2 "2019-12-18T18:39:24Z")

</div>

After diving into this forum over the past week or so, it looks like links with arbitrary names aren’t supported; only the link names from the specification are allowed.

Something like this might be more in keeping with the spec, keeping in mind that content negotiation is _the_ way to request specific formats:

```json
{
  ...
  "id": 1,
  "links": {
    "self": "https://api.com/resources/1",
    "meta": {
      "content_types": [
        "application/pdf",
        ...
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![panman82](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/panman82/32/789_2.png) [@panman82](https://discuss.jsonapi.org/u/panman82)\
**Post date:** [December 18, 2019, 9:52pm UTC](https://discuss.jsonapi.org/t/content-type-of-links/1553/3 "2019-12-18T21:52:47Z")

</div>

FWIW, I would add the Content-Type within the specific [links’ metadata](https://jsonapi.org/format/1.0/#document-links). Ex:

```json
{
  "id": "1",
  "links": {
    "self": "https://api.com/resources/1",
    "pdf": {
      "href": "https://api.com/resources/obfuscated-pdf-url",
      "meta": {
        "contentType": "application/pdf"
      }
    }
  }
}

```

OR, if the different Content-Types are at the same URL;

```json
{
  "id": "1",
  "links": {
    "self": {
      "href": "https://api.com/resources/1",
      "meta": {
        "contentTypes": [
          "application/vnd.api+json",
          "application/pdf"
        ]
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![fdrake](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/fdrake/32/654_2.png) [@fdrake](https://discuss.jsonapi.org/u/fdrake)\
**Post date:** [December 18, 2019, 9:55pm UTC](https://discuss.jsonapi.org/t/content-type-of-links/1553/4 "2019-12-18T21:55:09Z")

</div>

You’re right about using a link object with href and meta properties! Good catch.

---

<div class="post-metadata">

**Author:** ![maark](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/maark/32/184_2.png) [@maark](https://discuss.jsonapi.org/u/maark)\
**Post date:** [December 21, 2019, 8:40pm UTC](https://discuss.jsonapi.org/t/content-type-of-links/1553/5 "2019-12-21T20:40:33Z")

</div>

I don’t think this is valid JSON:API.

The Content-Type header must be `application/vnd.api+json`.

To provide assets like PDFs, this should be in its own API. For example, `api.example.com` is a JSON:API service and may give links to PDFs that can be retrieved through `static.example.com`.

It’s a similar issue to developers trying all sorts of things to handle authentication with JSON:API while session keys can simply be returned through another API like `auth.example.com` that follows whatever format makes sense.

---

<div class="post-metadata">

**Author:** ![fdrake](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/fdrake/32/654_2.png) [@fdrake](https://discuss.jsonapi.org/u/fdrake)\
**Post date:** [December 24, 2019, 6:27pm UTC](https://discuss.jsonapi.org/t/content-type-of-links/1553/6 "2019-12-24T18:27:02Z")

</div>

It’s not clear that other content types should not be available and negotiable at all. The “Content Negotiation” section specifies how the `application/vnd.api+json` should be unqualified, but does not require that all requests and responses have that media type.

Given how often the specification emphasizes that HTTP semantics should be followed, it would be surprising if the intent is to prevent content negotiation or to require that binary content is only available at a separate service name.

It would be nice if the spec were more clear on this matter, though.

---

<div class="post-metadata">

**Author:** ![panman82](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/panman82/32/789_2.png) [@panman82](https://discuss.jsonapi.org/u/panman82)\
**Post date:** [December 27, 2019, 7:08pm UTC](https://discuss.jsonapi.org/t/content-type-of-links/1553/7 "2019-12-27T19:08:52Z")

</div>

Sure, a request for a PDF wouldn’t be a _JSON:API_ request, but it would be nice to reference the URL somewhere in the resource document. Perhaps in the resource `meta` object instead @maark?

```auto
{
  "id": "1",
  "links": {
    "self": "https://api.com/resources/1"
  },
  "meta": {
    "pdf": "https://api.com/resources/obfuscated-pdf-url"
  }
}

```

---

<div class="post-metadata">

**Author:** ![onison](https://avatars.discourse-cdn.com/v4/letter/o/2bfe46/32.png) [@onison](https://discuss.jsonapi.org/u/onison)\
**Post date:** [December 5, 2023, 7:56pm UTC](https://discuss.jsonapi.org/t/content-type-of-links/1553/8 "2023-12-05T19:56:21Z")

</div>

When dealing with multiple representations of a resource in [different formats](https://converra.com/converter/docx/pdf) (such as JSON, HTML, and PDF), there are a few strategies you can use to specify the content type of links. Here are some recommendations:

### 1. Use the `Accept` Header:

- Clients can specify the desired content type using the `Accept` header in their HTTP request. The server can then respond with the appropriate representation based on the client’s preference.
- Example:

httpCopy code

```auto
GET /resources/1
Accept: application/json

```

### 2. Multiple `self` Links with Different Content Types:

- Include multiple `self` links in your response, each pointing to a different representation of the resource.
- Example:

jsonCopy code

```auto
{
  "id": 1,
  "links": {
    "self": "https://api.com/resources/1",
    "json": "https://api.com/resources/1.json",
    "html": "https://api.com/resources/1.html",
    "pdf": "https://api.com/resources/1.pdf"
  }
}

```

---

<div class="post-metadata">

**Author:** ![jelhan](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/jelhan/32/822_2.png) [@jelhan](https://discuss.jsonapi.org/u/jelhan)\
**Post date:** [December 9, 2023, 3:25pm UTC](https://discuss.jsonapi.org/t/content-type-of-links/1553/9 "2023-12-09T15:25:33Z")

</div>

> [@onison](#):
>
> Multiple `self` Links with Different Content Types

Please note that this violates the JSON:API specification. Implementations are not allowed to add new members to the `links` object defined by the specification.

---

<div class="post-metadata">

**Author:** ![Raheel](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@Raheel](https://discuss.jsonapi.org/u/Raheel)\
**Post date:** [January 15, 2024, 4:34pm UTC](https://discuss.jsonapi.org/t/content-type-of-links/1553/11 "2024-01-15T16:34:54Z")

</div>

I create a tool for online json validation or testing please check the [tool](https://digiraheel.com/) and give me feed back.

---

<div class="post-metadata">

**Author:** ![sabrinaa](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/sabrinaa/32/882_2.png) [@sabrinaa](https://discuss.jsonapi.org/u/sabrinaa)\
**Post date:** [July 18, 2024, 9:08am UTC](https://discuss.jsonapi.org/t/content-type-of-links/1553/12 "2024-07-18T09:08:06Z")

</div>

Hello,  
In my opinion, To specify content types for links, use the Link header in HTTP responses and an alternate links array in your JSON:

```auto
{
  "id": 1,
  "links": {
    "self": "https://api.com/resources/1",
    "alternate": [
      { "href": "https://api.com/resources/1", "type": "text/html" },
      { "href": "https://api.com/resources/obfuscated-pdf-url", "type": "application/pdf" }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![bradjones1](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/bradjones1/32/879_2.png) [@bradjones1](https://discuss.jsonapi.org/u/bradjones1)\
**Post date:** [July 20, 2024, 11:48pm UTC](https://discuss.jsonapi.org/t/content-type-of-links/1553/13 "2024-07-20T23:48:02Z")

</div>

This is well-described in the 1.1 spec: [JSON:API — Latest Specification (v1.1)](https://jsonapi.org/format/#auto-id--link-objects)
