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

Trying to convert API response to spit out XML
How to avoid scientific notation in circe JSON serialization
Golang Godog REST API Test fails
How to document json/js object for intellisense in vscode?
Laravel return query to single object
Swift: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) on accessing JSON URL
Passing the data of database in json object in laravel
spring restcontroller and responsebody returns list items like $ref
How do I fix the CRLF and LF warnings and permanently delete the index.lock file on the git bash command line?
How to consume a simple Restful service with Scala and Akka-Http 10.0.5
Merging two XML files on a key field
Jmeter - Json Extractor removing white space causes test to fail
Creating Dynamic HTML using json data in reactjs
API get return a loop of data in array format for json data
How to check if the change in nested data is permissible
Haproxy error 500 when use method POST with JSON, but works on backend

Categories

HOME
cil
vb6
checkbox
wmic
scipy
uibutton
ocaml
ubuntu-16.04
avro
amazon-product-api
schemacrawler
contact-form-7
openwrt
simple-injector
jacoco
riak
bootstrap-popover
session-timeout
factor-analysis
vs2017
pycrypto
cruisecontrol.net
dspic
azure-servicebus-queues
fabric
badge
samsung-mobile
python-textprocessing
sumo
geopandas
classpath
dpi
positioning
piecewise
rowcount
stocks
fractals
m2e
ocs
es-shell
sql-like
declare
installshield-2012
agent
dojox.mobile
finite-element-analysis
catia
rdw
tiddlywiki
quintus
selenium-firefoxdriver
jcreator
laravel-query-builder
debugdiag
nofollow
uwp-maps
remoteapp
concur
sun-codemodel
fill
except
acm
asymptote
optionbutton
futuretask
pyrocms
xjc
apache-mina
httpie
es2015
android-search
sysctl
formvalidation-plugin
funq
mbox
broadcasting
ksoap2
convex-polygon
nimrod
nscolor
execvp
haskell-platform
manage.py
fortran77
xslkey
android-contextmenu
phpsh
bulbs
osi
redirectstandardoutput
parameterization
noir
phpcrawl
clause
hardware-acceleration
revert
communicationexception
soft-keyboard
handheld
interface-design

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