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

406 Not Acceptable #32

Closed amanve closed 2 years ago

amanve commented 2 years ago

Hi,

whenever i try to GET response it returns 406 Not Acceptable. I'm using flask-sqlalchemy

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_restless import APIManager

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///restless_api.db'
db = SQLAlchemy(app)

class User(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  name = db.Column(db.String(20), unique=True)
  items = db.relationship('Item', backref='user')

class Item(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  name = db.Column(db.String(20), unique=True)
  user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

manager = APIManager(app, session=db.session)

manager.create_api(User, methods=['GET', 'POST'])
manager.create_api(Item, methods=['GET'])

if __name__ == '__main__':
  app.run()
amanve commented 2 years ago

Response-

{
  "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"
  }
}
mrevutskyi commented 2 years ago

Hi, could you please provide more details on how do you make that request? Do you pass the Accept header?

amanve commented 2 years ago

Hi, could you please provide more details on how do you make that request? Do you pass the Accept header?

Content-Length: 248 Content-Type: application/vnd.api+json Date: Tue, 15 Mar 2022 04:58:01 GMT Server: Werkzeug/2.0.3 Python/3.10.0

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,/;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.5 Connection: keep-alive Host: localhost:5000 Sec-Fetch-Dest: document Sec-Fetch-Mode: navigate Sec-Fetch-Site: none Sec-Fetch-User: ?1 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0

amanve commented 2 years ago

ok now i got it- https://flask-restless-ng.readthedocs.io/en/latest/requestformat.html application/vnd.api+json is the header

mrevutskyi commented 2 years ago

Right, so as you can see from the error's details, it expects Accept: application/vnd.api+json, not 'text/html' you can try curl -H 'Accept: application/vnd.api+json' http://...