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

How to insert JSON with an array into ORM
Get data from URL json in R
how to create json object in angular2
Extract value of single string record from PostgreSQL JSON column
Process json response in ruby
Getting 406 error when sending json response from controller
How To Intercept #RequestBody Binding
Why does my code panic when I type assert an interface?
Public VAT/BTW REST API
Custom Function to Extract Data from JSON API Based on Column Values in Excel VBA
“ЭThe app has not been configured with a WORKSPACE_ID environment variable” error for app on NodeJS in bluemix
Convert json list data's column datatype
Json Error In Laravel Ajax
How is India related to “Indira Gandhi”? (no need to find all existing relationships, but find at least an interesting one)
RAML - json schema error
Node Express & JSON Response Format

Categories

HOME
spring
plsql
ibm-watson-cognitive
vb6
itext
freepascal
wmic
wsdl
avro
command
ubuntu-12.04
csvhelper
terrain
browserify
unity2d
oclint
hosts
cortex-a
rebol2
lstm
zoomcharts
anychart
chocolatey
ejbca
shopping-cart
maximo
unpack
mayavi
reply
pep8-assembly
jackson-dataformat-csv
protobuf-net
ifstream
pycparser
superpowered
precedence
silverlight-3.0
soundjs
nsexception
mediawiki-extensions
pdftk
jdbi
nsjsonserialization
chown
chrome-remote-desktop
mbaas
ticker
info
restivejs
settimeout
cfeclipse
packagemaker
icefaces
gradle-eclipse
fps
project-online
date-format
logcat
git-rebase
anythingslider
jta
eyeql
jericho-html-parser
visual-studio-code
connection-reset
ytplayerview
web-component-tester
ibmsbt
docopt
firebug-lite
dto
funq
aquafold
smartystreets
alertifyjs
winrt-httpclient
chrome-for-android
gfs
bigint
nscolor
execvp
sttwitter
surveyor-gem
grunt-contrib-compass
shortcuts
process-monitor
fluidsynth
couchdb-lucene
radchart
law-of-demeter
hinstance
charts4j
gtk2hs
sendfile
hardware-acceleration
revert
grooveshark
webrat
oggvorbis
castle-validators
standardized

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