2016-02-11 34 views
1

關注此頁(https://django-oauth-toolkit.readthedocs.org/en/latest/rest-framework/getting_started.html),我能夠爲我的django項目設置OAuth。Curl -d to Alamofire

以下curl命令爲我提供了一個訪問資源的標記。

curl -X POST 
-d "grant_type=password&username=<user_name>&password=<password>" 
-u"<client_id>:<client_secret>" http://localhost:8000/o/token/ 

但是,當我使用Alamofire發送請求時,事情有點奇怪。這是我的代碼

Alamofire.request(.POST, url, parameters: parameters, encoding: .JSON) 
     .authenticate(user: client_ID, password: client_Secret) 

其中參數是一本字典

[ 
"password": <password>, 
"grant_type": password, 
"username": <username> 
] 

使用curl命令,我可以從Django中看到request.POST.items()返回的參數列表。但是,使用Alamofire,沒有什麼。參數出現在request.body中!

這個問題讓我發瘋。任何幫助將不勝感激!

在此先感謝。

回答

2

那麼,你的curl命令發佈爲Content-Type: application/x-www-form-urlencoded格式,而你正在強制發佈爲json(.JSON)。由於這個原因,請求已經作爲application/json傳遞給你的django,你正在看到的是參數而不是POST.items()

因此,從您的代碼encoding: .JSON中刪除此項。

+0

非常感謝!我昨晚想到了,現在事情進展順利! – Yinan