python - Can't DELETE when POST, PUT, and GET works, AngularJS-CORS, Flask-CORS -
i'm @ loss, cant understand on how come delete requests not coming through when post, put , works fine. , i'm pretty sure did every bit configuration necessary cors work. i'm using angularjs , flask-cors extension
and here's current work:
angular config:
angular.module('...' [...]).config(function($httpprovider) { $httpprovider.defaults.usexdomain = true; delete $httpprovider.defaults.headers.common['x-requested-with']; });
angular factory:
angular.module('...').factory('...'), function($http) { return { deleteelectronicaddress: function (partyid, contactmechanismid) { return $http({ url: urlbase + 'party/' + partyid + '/contact-mechanism/' + contactmechanismid, method: 'delete', }); }, somemoremethods: { ... } } }
my flask codes, (i'm using proposed app structure miguel grinberg on book flask web development)
config.py
class config: ... cors_headers = 'content-type' cors_resources = {r"/api/*": {"origins": "*"}} cors_methods = ['get', 'post', 'delete', 'put', 'options', 'head'] ...
project_folder/app/__init__.py:
from flask import flask flask.ext.sqlalchemy import sqlalchemy flask.ext.cors import cors config import config db = sqlalchemy() def create_app(config_name): app = flask(__name__) app.config.from_object(config[config_name]) config[config_name].init_app(app) db.init_app(app) cors(app) .api_1_0 import api api_1_0_blueprint app.register_blueprint(api_1_0_blueprint, url_prefix='/api/v1.0') return app
i'm using flask-cors v1.7.4.
project_folder/app/api/party_contact_mechanisms.py:
from datetime import date flask import jsonify .. import db ..models import partycontactmechanism . import api @api.route( '/party/<int:party_id>' + '/contact-mechanism/<int:contact_mechanism>', methods=['delete']) def unlink_party_contact_mechanism(party_id, contact_mechanism): """unlink contact mechanism onto party.""" party_contact = partycontactmechanism.query \ .filter_by(party_id=party_id) \ .filter_by(contact_mechanism_id=contact_mechanism) \ .first() party_contact.thru_date = str(date.today()) db.session.commit() return jsonify(success=true)
i tested httpie, both on local machine , on machine connected on lan, , works fine.
i'm running both angular , flask 0.0.0.0 host configuration can accessible on other machines connected on network.
here request , response headers when invoke browser
request headers:
options /api/v1.0/party/32232/contact-mechanism/80667 http/1.1 host: 10.61.18.217:5000 user-agent: mozilla/5.0 (x11; ubuntu; linux x86_64; rv:31.0) gecko/20100101 firefox/31.0 accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 accept-language: en-us,en;q=0.5 accept-encoding: gzip, deflate origin: http://0.0.0.0:9000 access-control-request-method: delete connection: keep-alive
response headers:
http/1.0 200 ok content-type: text/html; charset=utf-8 allow: post, options, delete access-control-allow-origin: * access-control-allow-methods: post access-control-allow-headers: content-type content-length: 0 server: werkzeug/0.9.6 python/2.7.6 date: mon, 18 aug 2014 07:20:18 gmt
here request , response headers when invoke httpie:
request headers:
delete /api/v1.0/party/32232/contact-mechanism/80667 http/1.1 accept: */* accept-encoding: gzip, deflate, compress content-length: 0 host: 10.61.18.217:5000 user-agent: httpie/0.8.0
response headers:
http/1.0 200 ok access-control-allow-headers: content-type access-control-allow-methods: delete, get, head, options, post, put access-control-allow-origin: * content-length: 21 content-type: application/json date: mon, 18 aug 2014 07:31:05 gmt server: werkzeug/0.9.6 python/2.7.6 { "success": true }
i got doing wrong now,
actually there's route on project_folder/app/api/party_contact_mechanisms.py
file didn’t bother adding on sample, since thought irrelevant (well though wrong)
here is:
@api.route( '/party/<int:party_id>' + '/contact-mechanism/<int:contact_mechanism>', methods=['post']) @cross_origin(methods=['post']) def link_party_contact_mechanism(party_id, contact_mechanism): """link contact mechanism onto party.""" party_contact = partycontactmechanism( party_id=party_id, contact_mechanism=contact_mechanism, from_date=str(date.today()), thru_date=none ) db.session.add(party_contact) db.session.commit() return jsonify({})
as can see has the same route rule different method, in here post.
and before used flask-cors v1.7 using v1.3, explains @cross_origin
decorator, since that's way make route cors enabled (or if want on routes, put on before_request
)
the thing missed new version is, yes can make whole app cors enabled initializing it, did on project_folder/app/__init__.py
, , yes can still specific routes via `@cross_origin', when do, overwrites global config, , in case have 2 routes same rule different methods , 1 having @cross_origin decorator , that's root of problem.
i deleted , works fine now. warned adding @cross_origin
decorator on other route wont fix it, seems reads first occurrence of it.
Comments
Post a Comment