python - How can i get django to stop returning "Response code: 403" to iPhone App -
i have following swift code iphone app:
var post:nsstring = "username=\(username)&password=\(password)" nslog("postdata: %@",post); var url:nsurl = nsurl(string: "http://localhost:8000/straightred/login/")! var postdata:nsdata = post.datausingencoding(nsasciistringencoding)! var postlength:nsstring = string( postdata.length ) var request:nsmutableurlrequest = nsmutableurlrequest(url: url) request.httpmethod = "post" request.httpbody = postdata request.setvalue(postlength, forhttpheaderfield: "content-length") request.setvalue("application/x-www-form-urlencoded", forhttpheaderfield: "content-type") request.setvalue("application/json", forhttpheaderfield: "accept") var reponseerror: nserror? var response: nsurlresponse? var urldata: nsdata? = nsurlconnection.sendsynchronousrequest(request, returningresponse:&response, error:&reponseerror) let res = response nshttpurlresponse!; nslog("response code: %ld", res.statuscode);
the code above gives me following result in xcode output window:
2015-04-22 20:30:03.768 swiftloginscreen[2290:70315] postdata: username=test&password=password
2015-04-22 20:30:03.821 swiftloginscreen[2290:70315] response code: 403
below django views.py part (although in honesty seems not matter put here error 403 returned before code run):
@require_post def login(request): username = request.post['username'] password = request.post['password'] user = authenticate(username=username, password=password) if user , user.is_active: return httpresponse("{\"succes:1\" }",content_type = 'application/json') else: return httpresponse("{\"success\":0,\"error_message\":\"invalid data\"}",content_type = 'application/json')
so, in summary, need add iphone app , / or django setup allow requests iphone app?
any appreciated, many thanks, alan.
sounds csrf protection related problem.
try decorating views accessed iphone app csrf_exempt.
Comments
Post a Comment