- Blind mode tutorial
lichess.org
Donate

Lichess API returns x-chess-pgn, even if get request asks for json

Hello,

I am trying to use the lichess API to get games in json format. However, even if I specify the content type as application/json in the get request, the API returns pgn.

How can I get it to return json as specified in the API documentation (https://lichess.org/api#tag/Games/operation/gamePgn)?

Example 1 using Invoke-RestMethod:
PS C:\Users\Admin-MO> Invoke-RestMethod -H @{"content-type"="application/json"} https://lichess.org/game/export/0RtRclR6 -v
VERBOSE: GET with 0-byte payload
VERBOSE: received 3612-byte response of content type application/x-chess-pgn
[Event "Rated Bullet game"]
[Site "https://lichess.org/0RtRclR6"]
[Date "2023.01.31"]
[White "someone"]
[Black "someoneelse"]
[Result "1-0"]
[UTCDate "2023.01.31"]
[UTCTime "16:59:02"]
[WhiteElo "2809"]
[BlackElo "2877"]
[WhiteRatingDiff "+7"]
[BlackRatingDiff "-7"]
[BlackTitle "IM"]
[Variant "Standard"]
[TimeControl "60+0"]
[ECO "C28"]
[Opening "Bishop's Opening: Vienna Hybrid, Hromádka Variation"]
[Termination "Time forfeit"]

Example 2 using curl:

PS C:\Users\Admin-MO> curl.exe -H "Content-Type: application/json" https://lichess.org/game/export/0RtRclR6 -v

  • Trying 152.228.187.173:443...
  • Connected to lichess.org (152.228.187.173) port 443 (#0)
  • schannel: disabled automatic use of client certificate
  • ALPN: offers http/1.1
  • ALPN: server accepted http/1.1

GET /game/export/0RtRclR6 HTTP/1.1
Host: lichess.org
User-Agent: curl/7.83.1
Accept: /
Content-Type: application/json

  • Mark bundle as not supporting multiuse
    < HTTP/1.1 200 OK
    < Server: nginx
    < Date: Tue, 31 Jan 2023 17:20:08 GMT
    < Content-Type: application/x-chess-pgn
    < Content-Length: 3612
    < Connection: keep-alive
    < Vary: Origin
    < Content-Disposition: attachment; filename=lichess_pgn_2023.01.31_doreality1991_vs_beatles_21.0RtRclR6.pgn
    < Access-Control-Allow-Origin: *
    < Access-Control-Allow-Headers: Origin, Authorization, If-Modified-Since, Cache-Control, Content-Type
    < Access-Control-Allow-Methods: OPTIONS, GET, POST, DELETE
    < Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
    < X-Frame-Options: DENY
    < Permissions-Policy: interest-cohort=()
    <
    [Event "Rated Bullet game"]
    [Site "https://lichess.org/0RtRclR6"]
    [Date "2023.01.31"]
    [White "someone"]
    [Black "someoneelse"]
    [Result "1-0"]
    [UTCDate "2023.01.31"]
    [UTCTime "16:59:02"]
    [WhiteElo "2809"]
    [BlackElo "2877"]
    [WhiteRatingDiff "+7"]
    [BlackRatingDiff "-7"]
    [BlackTitle "IM"]
    [Variant "Standard"]
    [TimeControl "60+0"]
    [ECO "C28"]
    [Opening "Bishop's Opening: Vienna Hybrid, Hromádka Variation"]
    [Termination "Time forfeit"]
Hello, I am trying to use the lichess API to get games in json format. However, even if I specify the content type as application/json in the get request, the API returns pgn. How can I get it to return json as specified in the API documentation (https://lichess.org/api#tag/Games/operation/gamePgn)? Example 1 using Invoke-RestMethod: PS C:\Users\Admin-MO> Invoke-RestMethod -H @{"content-type"="application/json"} https://lichess.org/game/export/0RtRclR6 -v VERBOSE: GET with 0-byte payload VERBOSE: received 3612-byte response of content type application/x-chess-pgn [Event "Rated Bullet game"] [Site "https://lichess.org/0RtRclR6"] [Date "2023.01.31"] [White "someone"] [Black "someoneelse"] [Result "1-0"] [UTCDate "2023.01.31"] [UTCTime "16:59:02"] [WhiteElo "2809"] [BlackElo "2877"] [WhiteRatingDiff "+7"] [BlackRatingDiff "-7"] [BlackTitle "IM"] [Variant "Standard"] [TimeControl "60+0"] [ECO "C28"] [Opening "Bishop's Opening: Vienna Hybrid, Hromádka Variation"] [Termination "Time forfeit"] Example 2 using curl: PS C:\Users\Admin-MO> curl.exe -H "Content-Type: application/json" https://lichess.org/game/export/0RtRclR6 -v * Trying 152.228.187.173:443... * Connected to lichess.org (152.228.187.173) port 443 (#0) * schannel: disabled automatic use of client certificate * ALPN: offers http/1.1 * ALPN: server accepted http/1.1 > GET /game/export/0RtRclR6 HTTP/1.1 > Host: lichess.org > User-Agent: curl/7.83.1 > Accept: */* > Content-Type: application/json > * Mark bundle as not supporting multiuse < HTTP/1.1 200 OK < Server: nginx < Date: Tue, 31 Jan 2023 17:20:08 GMT < Content-Type: application/x-chess-pgn < Content-Length: 3612 < Connection: keep-alive < Vary: Origin < Content-Disposition: attachment; filename=lichess_pgn_2023.01.31_doreality1991_vs_beatles_21.0RtRclR6.pgn < Access-Control-Allow-Origin: * < Access-Control-Allow-Headers: Origin, Authorization, If-Modified-Since, Cache-Control, Content-Type < Access-Control-Allow-Methods: OPTIONS, GET, POST, DELETE < Strict-Transport-Security: max-age=63072000; includeSubDomains; preload < X-Frame-Options: DENY < Permissions-Policy: interest-cohort=() < [Event "Rated Bullet game"] [Site "https://lichess.org/0RtRclR6"] [Date "2023.01.31"] [White "someone"] [Black "someoneelse"] [Result "1-0"] [UTCDate "2023.01.31"] [UTCTime "16:59:02"] [WhiteElo "2809"] [BlackElo "2877"] [WhiteRatingDiff "+7"] [BlackRatingDiff "-7"] [BlackTitle "IM"] [Variant "Standard"] [TimeControl "60+0"] [ECO "C28"] [Opening "Bishop's Opening: Vienna Hybrid, Hromádka Variation"] [Termination "Time forfeit"]

Hi!

Try changing your request header from "content-type" to "accept".

("accept" in the request header says that you want the server to respond in that format,
while "content-type" in the request header says that any data you send in your request is encoded in that format - but you are not sending any data)

Chess on!

Hi! Try changing your request header from "content-type" to "accept". ("accept" in the request header says that you want the server to respond in that format, while "content-type" in the request header says that any data you send in your request is encoded in that format - but you are not sending any data) Chess on!

Thank you, that of course solved it :)

Thank you, that of course solved it :)

Niiice woodwindblues!

Now you're a Master of the Lichess API, woop woop!
Use this skill wisely. :)

Chess on!

Niiice woodwindblues! Now you're a Master of the Lichess API, woop woop! Use this skill wisely. :) Chess on!

This topic has been archived and can no longer be replied to.