zmb3 / spotify

A Go wrapper for the Spotify Web API
Apache License 2.0
1.34k stars 284 forks source link

How to set accept-language header in order to support multiple languages? #130

Closed p1ass closed 3 years ago

p1ass commented 3 years ago

Spotify Web API returns responses according to accept-language HTTP header. (This is the independent of the market query parameter. It sets whether songs or artists are playable in that market, but accept-language sets translation.) So, we would like to set accept-language before API call, but I don't know how to set.

https://developer.spotify.com/documentation/web-api/reference-beta/#category-search

accept-language: en,ja;q=0.9

$ curl -s 'https://api.spotify.com/v1/search?q=alan%20walker&market=JP&type=artist&limit=1' \
  -H 'authorization: Bearer xxx' \
  -H 'accept-language: en,ja;q=0.9' \
  --compressed | jq .
{
  "artists": {
    "href": "https://api.spotify.com/v1/search?query=alan+walker&type=artist&offset=0&limit=1",
    "items": [
      {
        "external_urls": {
          "spotify": "https://open.spotify.com/artist/7vk5e3vY1uw9plTHJAMwjN"
        },
        "followers": {
          "href": null,
          "total": 22794957
        },
        "genres": [
          "electro house"
        ],
        "href": "https://api.spotify.com/v1/artists/7vk5e3vY1uw9plTHJAMwjN",
        "id": "7vk5e3vY1uw9plTHJAMwjN",
        "images": [
          {
            "height": 640,
            "url": "https://i.scdn.co/image/86213c012b11a646e3c8c67c7aa093cccf7e6b48",
            "width": 640
          },
          {
            "height": 320,
            "url": "https://i.scdn.co/image/71ab34f044b49e006d70df90fdd636032e732469",
            "width": 320
          },
          {
            "height": 160,
            "url": "https://i.scdn.co/image/5b253facb0bca7172baec8c0d17a4eedb9799d75",
            "width": 160
          }
        ],
        "name": "Alan Walker", // English
        "popularity": 85,
        "type": "artist",
        "uri": "spotify:artist:7vk5e3vY1uw9plTHJAMwjN"
      }
    ],
    "limit": 1,
    "next": "https://api.spotify.com/v1/search?query=alan+walker&type=artist&offset=1&limit=1",
    "offset": 0,
    "previous": null,
    "total": 12
  }
}

accept-language: ja,en;q=0.9

$ curl -s 'https://api.spotify.com/v1/search?q=alan%20walker&market=JP&type=artist&limit=1' \
  -H 'authorization: Bearer xxx' \
  -H 'accept-language: ja,en;q=0.9' \
  --compressed | jq .
{
  "artists": {
    "href": "https://api.spotify.com/v1/search?query=alan+walker&type=artist&offset=0&limit=1",
    "items": [
      {
        "external_urls": {
          "spotify": "https://open.spotify.com/artist/7vk5e3vY1uw9plTHJAMwjN"
        },
        "followers": {
          "href": null,
          "total": 22794957
        },
        "genres": [
          "electro house"
        ],
        "href": "https://api.spotify.com/v1/artists/7vk5e3vY1uw9plTHJAMwjN",
        "id": "7vk5e3vY1uw9plTHJAMwjN",
        "images": [
          {
            "height": 640,
            "url": "https://i.scdn.co/image/86213c012b11a646e3c8c67c7aa093cccf7e6b48",
            "width": 640
          },
          {
            "height": 320,
            "url": "https://i.scdn.co/image/71ab34f044b49e006d70df90fdd636032e732469",
            "width": 320
          },
          {
            "height": 160,
            "url": "https://i.scdn.co/image/5b253facb0bca7172baec8c0d17a4eedb9799d75",
            "width": 160
          }
        ],
        "name": "アラン・ウォーカー", // Japanese
        "popularity": 85,
        "type": "artist",
        "uri": "spotify:artist:7vk5e3vY1uw9plTHJAMwjN"
      }
    ],
    "limit": 1,
    "next": "https://api.spotify.com/v1/search?query=alan+walker&type=artist&offset=1&limit=1",
    "offset": 0,
    "previous": null,
    "total": 12
  }
}