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

Loop through list of URLs using .Open (Parsing JSON in VBA)
Get unique records where column type is JSONB
Convert complex JSON (with arrays and different data types) to CSV using JQ 1.4
is there away to make json return the search for one character?
How to parse to TDateTime a date time returned from a JSON String in Delphi XE 10 [duplicate]
Extracting all the information from an uncommon JSON structure in R
XSLT output JSON instead of XML
ObjectMapper won't unwrap JSON for REST
Looping a request that returns results in order they were put in
How to convert array of dictionary to JSON?
Can not load the image from JSON using carousel bootstrap
How can I generate or import multiple json files in one, with webpack?
Firebase 3 orderByChild not working with ionic2
qml append json stream to listmodel
Read JSON Data With String Key: Angular2
Retrofit Expected BEGIN_ARRAY but was BEGIN_OBJECT exception

Categories

HOME
vue.js
winapi
wsdl
antivirus
ubuntu-16.04
decorator
computer-vision
javamail
algorithmic-trading
bro
vsftpd
actionscript-2
slim-3
dropbox
xul
dryioc
dlib
maxima
pdo
rhapsody
jive
backpropagation
nmake
google-ima
pipelinedb
excel-2013
version-numbering
vcard
pcre
datadog
ioc-container
protobuf-net
large-data
precedence
pace
oscommerce
boilerplate
vmd
issue-tracking
dart-pub
web-development-server
ical-dotnet
tripwire
finite-element-analysis
rich-text-editor
fqdn
appstore-approval
jags
gauss
web-deployment-project
windows-95
android-bitmap
info
jcreator
iron.io
polar-coordinates
barcode-printing
master
typhoon
debugdiag
glkit
children
spoofing
unsatisfiedlinkerror
image-editing
mercurial-hook
django-south
bluesnap
jta
jenkins-scriptler
programming-paradigms
connection-reset
type-mismatch
findcontrol
multipleselection
adcolony
ogr2ogr
dache
boost-test
lov
chrome-for-android
pyjade
confusion-matrix
cdata
dwolla
awesomeprint
reporting-tools
angularjs-timeout
ubuntu-11.10
git-filter-branch
css-tables
xmlslurper
maven-ant-tasks
driving-directions
surf
datacontract
goliath
authenticode
sendfile
carbide
plinq
callgrind
inline-if
hardware-infrastructure





Mobile Apps Dev
Database Users
javascript
java
csharp
php
android


MS Developer
developer works
python
ios
c
html
jquery


RDBMS discuss