내 API에 django-rest-framework을 사용하고 있습니다. 내 API에 데이터를 게시하려면 먼저 사용자가 자신의 자격 증명으로 로그인해야합니다. 명령 행 게시물에서
는 다음과 같은 :
curl -X POST http://127.0.0.1:8000/snippets/ -d "code=print 789" -u username:password
{"id": 5, "owner": "tom", "title": "foo", "code": "print 789", "linenos": false, "language": "python", "style": "friendly"}
을 지금, AngularJS와 함께 데이터를 게시하려합니다. 다음은 코드입니다.
$http.post('http://localhost:8000/snippets/', self.newSnippet)
.then(fetchSnippets())
.then(function (response) {
self.newSnippet = {};
})
물론 로그인하지 않았으므로이 데이터를 게시 할 수 없습니다.
질문 :
어떻게이 $ http.post와 사용자 자격 증명을 보내합니까 명령 줄에서 컬 (-u 사용자 이름 : 암호)와 함께했던 것처럼? 이 HTTP 기본 인증의 경우
휴식 API를 테스트하려면 django-swagger를 시도하십시오 –
REST 환경에서 인증하려면 몇 가지 특별한주의가 필요합니다. DRF 문서 (http://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/)를보십시오. 서버에서이 API를 사용할 수있는 경우 로그인보기를 직접 호출 할 수도 있습니다 (https://docs.djangoproject.com/en/1.7/topics/auth/default/#howtoto-log-a-user-in). –