Curl -d to Alamofire

倾然丶 夕夏残阳落幕 提交于 2019-12-11 10:39:28

问题


Following this page (https://django-oauth-toolkit.readthedocs.org/en/latest/rest-framework/getting_started.html), I was able to setup OAuth for my django project.

The following curl command gives me a token to access resource.

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

However, when I send request using Alamofire, things are a bit strange. This is my code

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

where parameter is a dictionary

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

Using curl command, I can see from Django that request.POST.items() returns the list of parameters. However, using Alamofire, there is nothing. The parameters appeared in request.body instead!

This problem is driving me crazy. Any help will be grateful!

Thanks in advance.


回答1:


Well, your curl command is posting as Content-Type: application/x-www-form-urlencoded format, whereas you are forcing to post as json (.JSON). For this reason the request has passed as application/json to your django and you are seeing the parameter in body instead of POST.items().

So remove this from your code encoding: .JSON.



来源:https://stackoverflow.com/questions/35332221/curl-d-to-alamofire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!