json


Basic auth header in Alamofire


I have a protected api and I got the username and password for it. When I use Postman I select the "Basic Auth" type and in the body I have the parameters such as the user login info. It works just fine.
However I'm trying to do the same thing with Alamofire but I can't get the right JSON return. Here is what I did :
// user auth
let param = ["mobile":"3", "password":"100200"]
let urlStr = "http://MyApi.com/api/login"
let url = URL(string: urlStr)
// api auth
let user = "apiUserName"
let password = "ApiAuthPassword"
var headers: HTTPHeaders = ["mobile":"001",
"password":"1111"]
if let authorizationHeader = Request.authorizationHeader(user: user, password: password) {
headers[authorizationHeader.key] = authorizationHeader.value
}
Alamofire.request(url!, headers: headers)
.responseJSON { response in
print(response.result.value)
if let value: AnyObject = response.result.value as AnyObject? {
//Handle the results as JSON
print(value)
let usertoken = JSON(value)
print(usertoken)
}
}
This returns nil. Can someone help me how to do the Postman method with Alamofire. Thanks!
let user = "your username"
let password = "your password"
let credentialData = "(user):(password)".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
let base64Credentials = credentialData.base64EncodedString()
let headers = [
"Authorization": "Basic (base64Credentials)",
"Accept": "application/json",
"Content-Type": "application/json" ]
//put slash before \ (base64Credentials)
Alamofire.request("place your url", method: .get, parameters: nil,encoding: URLEncoding.default, headers: headers) .responseJSON { response in
switch response.result {
case .success(_):
//your success code
}
case .failure(_):
//your failure code
}
}

Related Links

Ionic 2 virtual scroll with objects
play json for nested json processing
systemjs configuration error leading to a file not found error
how to process JSON from Axios call in Scala
Extract JSON fields without looping in Talend
Adding no arg constructor to Scala enumerations
Get only the content of each commit in a json ? ( git diff json)
Parse specific output from json with jq
No Matching Action Clause to Process Request error on HTTPoison.patch
Creating JAR file with Maven and bundling json files into it
How to get Twitter organic impressions data from Twitter analytics API
How to reduce whitespace from JSON on the command line without installing a tool
Parsing a JSON string that is bigger than the memory
Why I don't get any response in console log?
JSON structure output
SQL 2008 remove smart quotes before data insert

Categories

HOME
mql4
voip
webrtc
cocos2d-x-3.0
parse-server
tomcat7
sbt-assembly
orientation
defragmentation
dataframe
shipping
uiscrollview
store
phoenix
uicollectionview
quartus
plupload
aws-cognito
cruisecontrol.net
tflearn
yosys
scalajs-react
python-textprocessing
jenkins-job-dsl
metadata-extractor
multichoiceitems
positioning
superscript
ioc-container
getjson
postgresql-9.2
body-parser
rhomobile
pox
oscommerce
swagger-editor
mongoexport
microdata
pytest-django
worker
z3py
bids
ajp
timesten
proof
broadcast
ogre3d
windows-95
slidesjs
google-maps-ios
objloader
master
tomee
mix
webkit2
spark-cassandra-connector
contour
multilingual-app-toolkit
distribute
post-processing
aerogear
axes
git-rebase
loose-typing
java.util.calendar
uialertview
windowlistener
abas
ipojo
gray-code
google-hadoop
low-level
multipleselection
tilestache
alphablending
errorprovider
surrogate-key
coin-flipping
sgml
xenocode
facebook-chat
gamepad
ng-hide
execvp
ruboto
lame
digital-design
pascals-triangle
android-4.0
dropdownlistfor
windows-phone-7-emulator
gjs
ubuntu-11.04
shared-objects
curljs
junit3
sitemappath
file-encodings
scala-2.8
google-instant
winverifytrust
openvg
usergroups

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App