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

Angular2 cast a json result to an interface
Embedded JSON struct
how to do autocomplete in angularjs using JSON
How to use custom JSON attributes in Chef recipe
export and import data from mongodb using node.js express
I am working on geojson file with 10000 features now in order to plot heatmaps I need to modify all 10000 features any efficient way to do this?
Typescript internal module not recognized by VS Code
How to loop through JSON in ruby
How to get a json key from a Sequence File in pig
How to use play-json format/reads/writes macro with tagged types
Bootstrap Table – Loading Data from JSON Object Property
google map JSON.parse: bad escaped character at line 1 column 42 of the JSON data
JSON regex pattern
Restrict the size of POST request (application/json) in tomcat 7
Golang JSON Encoder and nginx proxy buffering
Send Json files consists of many rows from web server applications to mobile applications and vice versa

Categories

HOME
shopify
reverse-engineering
itext
drupal-7
wsdl
google-tag-manager
fparsec
d
thunderbird-addon
playframework
bibtex
search-engine
orientation
iis-7.5
tizen-wearable-sdk
sudo
gimp
constructor
hashicorp-vault
game-maker-studio-1.4
game-physics
store
ejs
redux-observable
google-people
vs2017
sensu
spring-ws
binutils
interrupt-handling
i3
aurelia-binding
vertex-buffer
protobuf-net
apache-cloudstack
pycparser
ocs
recurrence-relation
wfp
code-behind
meta-raspberrypi
ical-dotnet
xbim
stereo-3d
constants
homekit
mod-fcgid
fastq
email-parsing
shutdown
tcserver
instruction-set
selenium-firefoxdriver
restivejs
radians
coordinate-transformation
elastix
picking
post-processing
xmp
android-cursoradapter
ninject.web.mvc
app.xaml
oai
retro-computing
android-snackbar
abas
team-build
lines-of-code
uos
ember-cli-addons
viewflipper
proximity
cloudpebble
collabnet
ember-components
log4cplus
wss
dvcs
mimosa
bigint
trimming
sttwitter
manage.py
dynamics-ax-2009
abnf
mvs
cbcentralmanager
linkedhashset
usn
moq-3
couchdb-lucene
static-variables
cosm
nemerle
coff
goliath
nagle
libavformat
clause
gwt-2.2-celltable
internals
cots





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