问题
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