mrevutskyi / flask-restless-ng

A Flask extension for creating simple ReSTful JSON APIs from SQLAlchemy models.
https://flask-restless-ng.readthedocs.io
Other
64 stars 11 forks source link

Question about the JSON API headers #8

Closed cedricbonhomme closed 1 year ago

cedricbonhomme commented 3 years ago

First want to thank you for this fork. I am using Flask-Restless since years. And it seems that finally there is a good candidate as replacement. Thank you for this.

I have a small question about this the example below. Could explain me why ? Is it a good practice or something ?

$ curl http://127.0.0.1:5000/api/person
{
  "errors": [
    {
      "code": null, 
      "detail": "Accept header, if specified, must be the JSON API media type: application/vnd.api+json", 
      "id": null, 
      "links": null, 
      "meta": null, 
      "source": null, 
      "status": 406, 
      "title": null
    }
  ], 
  "jsonapi": {
    "version": "1.0"
  }, 
  "meta": {}
}

$ curl -H "Accept: application/vnd.api+json" http://127.0.0.1:5000/api/person
{
  "data": [], 
  "jsonapi": {
    "version": "1.0"
  }, 
  "links": {
    "first": "http://127.0.0.1:5000/api/person?page[size]=10&page[number]=1", 
    "last": "http://127.0.0.1:5000/api/person?page[size]=10&page[number]=0", 
    "next": null, 
    "prev": null, 
    "self": "/api/person"
  }, 
  "meta": {
    "total": 0
  }
}

Cheers,

Cédric

mrevutskyi commented 3 years ago

Hi,

thank you for your question. I've looked at the JSON API specs and there is a little bit a of a gray area about the Accept header

https://jsonapi.org/format/#content-negotiation-clients

On one hand it says: Servers MUST respond with a 406 Not Acceptable status code if a request’s Accept header contains the JSON:API media type and all instances of that media type are modified with media type parameters.

but it does not say what server should do if request does not have Accept header at all (and so no 'media type parameters')

Seems like other people are also confused: https://discuss.jsonapi.org/t/content-type-accept-header-error-responses/1667

I'll try to clarify what is correct server response should be if Accept header is not present, but it looks like it should not be required. Thought it does require Content-Type: application/vnd.api+json

cedricbonhomme commented 3 years ago

thank you for your answer.

Actually I never had do do this, in my whole life ;-)

But thanks, I wait for your clarification.