IOS - Swift 4.0 - Basic access authentication with Alamofire -
up vote
2
down vote
favorite
I'm doing an app in Swift 4 i when I get to the point where I need to make a POST call, like "Basic access authentication" with a header and a body with parameters, it does not work for me.
Basically I want to simulate this call, to return a 200, but it's giving me back:
"responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error
Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character
0." UserInfo={NSDebugDescription=Invalid value around character 0.})) "
The API url is:
https://gist.github.com/juaniiton1/3a3e422ed99de22f3f3686bb3b7788d7
and the section is "Store a new trade in the authed user portfolio".
Here I pass the code that simulates the request "Store a new trade in the authoved user portfolio" with the help of Alamofire (4.7.3).
The steps I follow are the following:
What I do is, code the data of "username" and "password" that you
give me in base64.Then I format the date of type Stirng to type Date (type ISO).
After I created a dictionary for the data
And finally I created the request, putting together all of the above.
In addition in the Xcode I have the TransportSecurity to make the
request "https"
Here I leave the code:
import Foundation
import UIKit
import Alamofire
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//URL
let url = URL(string: "https://test.cryptojet.io/coins/portfolio")!
// credentials encoded in base64
let username = "richard@rich.com"
let password = "secret"
let loginData = String(format: "%@:%@", username, password).data(using: String.Encoding.utf8)!
let base64LoginData = loginData.base64EncodedString()
let headers = ["Authorization" : "Basic (base64LoginData)",
"Content-Type": "application/json"]
//Prepare date - Data UTC to ISO
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
let dataSting = "2016-06-05T16:56:57.019+01:00"
let dateISO = dateFormatter.date(from: dataSting)
//Prepare paramsBody -> tot Data
let paramsBody: [String: Any] = ["coin_id": 2,
"amount": -2.2183,
"price_usd": 675.982,
"traded_at": dateISO!]
//Request
Alamofire.request(url, method: .post,parameters: paramsBody, encoding: URLEncoding.default, headers: headers).responseJSON { response in
switch response.result {
case .success:
guard let json = response.result.value as? [String: Any],
let data = json["trade"] as? [String: Any] else {
return
}
print(data)
case .failure(let error):
print(error)
}
}
}
Could you tell me what I'm doing wrong with this POST authentication call with the server?
ios swift post alamofire basic-authentication
add a comment |
up vote
2
down vote
favorite
I'm doing an app in Swift 4 i when I get to the point where I need to make a POST call, like "Basic access authentication" with a header and a body with parameters, it does not work for me.
Basically I want to simulate this call, to return a 200, but it's giving me back:
"responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error
Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character
0." UserInfo={NSDebugDescription=Invalid value around character 0.})) "
The API url is:
https://gist.github.com/juaniiton1/3a3e422ed99de22f3f3686bb3b7788d7
and the section is "Store a new trade in the authed user portfolio".
Here I pass the code that simulates the request "Store a new trade in the authoved user portfolio" with the help of Alamofire (4.7.3).
The steps I follow are the following:
What I do is, code the data of "username" and "password" that you
give me in base64.Then I format the date of type Stirng to type Date (type ISO).
After I created a dictionary for the data
And finally I created the request, putting together all of the above.
In addition in the Xcode I have the TransportSecurity to make the
request "https"
Here I leave the code:
import Foundation
import UIKit
import Alamofire
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//URL
let url = URL(string: "https://test.cryptojet.io/coins/portfolio")!
// credentials encoded in base64
let username = "richard@rich.com"
let password = "secret"
let loginData = String(format: "%@:%@", username, password).data(using: String.Encoding.utf8)!
let base64LoginData = loginData.base64EncodedString()
let headers = ["Authorization" : "Basic (base64LoginData)",
"Content-Type": "application/json"]
//Prepare date - Data UTC to ISO
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
let dataSting = "2016-06-05T16:56:57.019+01:00"
let dateISO = dateFormatter.date(from: dataSting)
//Prepare paramsBody -> tot Data
let paramsBody: [String: Any] = ["coin_id": 2,
"amount": -2.2183,
"price_usd": 675.982,
"traded_at": dateISO!]
//Request
Alamofire.request(url, method: .post,parameters: paramsBody, encoding: URLEncoding.default, headers: headers).responseJSON { response in
switch response.result {
case .success:
guard let json = response.result.value as? [String: Any],
let data = json["trade"] as? [String: Any] else {
return
}
print(data)
case .failure(let error):
print(error)
}
}
}
Could you tell me what I'm doing wrong with this POST authentication call with the server?
ios swift post alamofire basic-authentication
Are you trying to use digest protocol? if this is the issue you need to use alamo in a different way
– ironRoei
Nov 19 at 16:03
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm doing an app in Swift 4 i when I get to the point where I need to make a POST call, like "Basic access authentication" with a header and a body with parameters, it does not work for me.
Basically I want to simulate this call, to return a 200, but it's giving me back:
"responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error
Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character
0." UserInfo={NSDebugDescription=Invalid value around character 0.})) "
The API url is:
https://gist.github.com/juaniiton1/3a3e422ed99de22f3f3686bb3b7788d7
and the section is "Store a new trade in the authed user portfolio".
Here I pass the code that simulates the request "Store a new trade in the authoved user portfolio" with the help of Alamofire (4.7.3).
The steps I follow are the following:
What I do is, code the data of "username" and "password" that you
give me in base64.Then I format the date of type Stirng to type Date (type ISO).
After I created a dictionary for the data
And finally I created the request, putting together all of the above.
In addition in the Xcode I have the TransportSecurity to make the
request "https"
Here I leave the code:
import Foundation
import UIKit
import Alamofire
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//URL
let url = URL(string: "https://test.cryptojet.io/coins/portfolio")!
// credentials encoded in base64
let username = "richard@rich.com"
let password = "secret"
let loginData = String(format: "%@:%@", username, password).data(using: String.Encoding.utf8)!
let base64LoginData = loginData.base64EncodedString()
let headers = ["Authorization" : "Basic (base64LoginData)",
"Content-Type": "application/json"]
//Prepare date - Data UTC to ISO
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
let dataSting = "2016-06-05T16:56:57.019+01:00"
let dateISO = dateFormatter.date(from: dataSting)
//Prepare paramsBody -> tot Data
let paramsBody: [String: Any] = ["coin_id": 2,
"amount": -2.2183,
"price_usd": 675.982,
"traded_at": dateISO!]
//Request
Alamofire.request(url, method: .post,parameters: paramsBody, encoding: URLEncoding.default, headers: headers).responseJSON { response in
switch response.result {
case .success:
guard let json = response.result.value as? [String: Any],
let data = json["trade"] as? [String: Any] else {
return
}
print(data)
case .failure(let error):
print(error)
}
}
}
Could you tell me what I'm doing wrong with this POST authentication call with the server?
ios swift post alamofire basic-authentication
I'm doing an app in Swift 4 i when I get to the point where I need to make a POST call, like "Basic access authentication" with a header and a body with parameters, it does not work for me.
Basically I want to simulate this call, to return a 200, but it's giving me back:
"responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error
Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character
0." UserInfo={NSDebugDescription=Invalid value around character 0.})) "
The API url is:
https://gist.github.com/juaniiton1/3a3e422ed99de22f3f3686bb3b7788d7
and the section is "Store a new trade in the authed user portfolio".
Here I pass the code that simulates the request "Store a new trade in the authoved user portfolio" with the help of Alamofire (4.7.3).
The steps I follow are the following:
What I do is, code the data of "username" and "password" that you
give me in base64.Then I format the date of type Stirng to type Date (type ISO).
After I created a dictionary for the data
And finally I created the request, putting together all of the above.
In addition in the Xcode I have the TransportSecurity to make the
request "https"
Here I leave the code:
import Foundation
import UIKit
import Alamofire
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//URL
let url = URL(string: "https://test.cryptojet.io/coins/portfolio")!
// credentials encoded in base64
let username = "richard@rich.com"
let password = "secret"
let loginData = String(format: "%@:%@", username, password).data(using: String.Encoding.utf8)!
let base64LoginData = loginData.base64EncodedString()
let headers = ["Authorization" : "Basic (base64LoginData)",
"Content-Type": "application/json"]
//Prepare date - Data UTC to ISO
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
let dataSting = "2016-06-05T16:56:57.019+01:00"
let dateISO = dateFormatter.date(from: dataSting)
//Prepare paramsBody -> tot Data
let paramsBody: [String: Any] = ["coin_id": 2,
"amount": -2.2183,
"price_usd": 675.982,
"traded_at": dateISO!]
//Request
Alamofire.request(url, method: .post,parameters: paramsBody, encoding: URLEncoding.default, headers: headers).responseJSON { response in
switch response.result {
case .success:
guard let json = response.result.value as? [String: Any],
let data = json["trade"] as? [String: Any] else {
return
}
print(data)
case .failure(let error):
print(error)
}
}
}
Could you tell me what I'm doing wrong with this POST authentication call with the server?
ios swift post alamofire basic-authentication
ios swift post alamofire basic-authentication
asked Aug 24 at 8:33
Jordi Gallen
255
255
Are you trying to use digest protocol? if this is the issue you need to use alamo in a different way
– ironRoei
Nov 19 at 16:03
add a comment |
Are you trying to use digest protocol? if this is the issue you need to use alamo in a different way
– ironRoei
Nov 19 at 16:03
Are you trying to use digest protocol? if this is the issue you need to use alamo in a different way
– ironRoei
Nov 19 at 16:03
Are you trying to use digest protocol? if this is the issue you need to use alamo in a different way
– ironRoei
Nov 19 at 16:03
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
I tried to make request to your url and seems response from your request is not JSON:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex,nofollow" />
<style> body { background-color: #F9F9F9; color: #222; font: 14px/1.4 Helvetica, Arial, sans-serif; margin: 0; padding-bottom: 45px; }
a { cursor: pointer; text-decoration: none; }
a:hover { text-decoration: underline; }
...
Yeah, it seems that the API does not work with POST type, with the GET type it does return JSON response.
– Martin Prusa
Aug 24 at 9:04
Yes, the API does work in POST. I know because the one who gave me the test says: "I see where the error is but you will understand that if I tell you the solution, the test would be grace since it is a problem of the code and not of the backend. .. "
– Jordi Gallen
Aug 24 at 9:37
add a comment |
up vote
0
down vote
If you are trying digest you will need this method :
credential = URLCredential(user: UserDefaults.standard.string(forKey: "userName") ?? "admin", password: UserDefaults.standard.string(forKey: "password") ?? "admin", persistence: .none)
let sessionMananager = Alamofire.SessionManager.default
_ = sessionMananager.request(url)
.authenticate(usingCredential: credential!)
.responseJSON { response in
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I tried to make request to your url and seems response from your request is not JSON:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex,nofollow" />
<style> body { background-color: #F9F9F9; color: #222; font: 14px/1.4 Helvetica, Arial, sans-serif; margin: 0; padding-bottom: 45px; }
a { cursor: pointer; text-decoration: none; }
a:hover { text-decoration: underline; }
...
Yeah, it seems that the API does not work with POST type, with the GET type it does return JSON response.
– Martin Prusa
Aug 24 at 9:04
Yes, the API does work in POST. I know because the one who gave me the test says: "I see where the error is but you will understand that if I tell you the solution, the test would be grace since it is a problem of the code and not of the backend. .. "
– Jordi Gallen
Aug 24 at 9:37
add a comment |
up vote
0
down vote
I tried to make request to your url and seems response from your request is not JSON:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex,nofollow" />
<style> body { background-color: #F9F9F9; color: #222; font: 14px/1.4 Helvetica, Arial, sans-serif; margin: 0; padding-bottom: 45px; }
a { cursor: pointer; text-decoration: none; }
a:hover { text-decoration: underline; }
...
Yeah, it seems that the API does not work with POST type, with the GET type it does return JSON response.
– Martin Prusa
Aug 24 at 9:04
Yes, the API does work in POST. I know because the one who gave me the test says: "I see where the error is but you will understand that if I tell you the solution, the test would be grace since it is a problem of the code and not of the backend. .. "
– Jordi Gallen
Aug 24 at 9:37
add a comment |
up vote
0
down vote
up vote
0
down vote
I tried to make request to your url and seems response from your request is not JSON:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex,nofollow" />
<style> body { background-color: #F9F9F9; color: #222; font: 14px/1.4 Helvetica, Arial, sans-serif; margin: 0; padding-bottom: 45px; }
a { cursor: pointer; text-decoration: none; }
a:hover { text-decoration: underline; }
...
I tried to make request to your url and seems response from your request is not JSON:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex,nofollow" />
<style> body { background-color: #F9F9F9; color: #222; font: 14px/1.4 Helvetica, Arial, sans-serif; margin: 0; padding-bottom: 45px; }
a { cursor: pointer; text-decoration: none; }
a:hover { text-decoration: underline; }
...
answered Aug 24 at 8:47
Vitaliy Gozhenko
6,19423053
6,19423053
Yeah, it seems that the API does not work with POST type, with the GET type it does return JSON response.
– Martin Prusa
Aug 24 at 9:04
Yes, the API does work in POST. I know because the one who gave me the test says: "I see where the error is but you will understand that if I tell you the solution, the test would be grace since it is a problem of the code and not of the backend. .. "
– Jordi Gallen
Aug 24 at 9:37
add a comment |
Yeah, it seems that the API does not work with POST type, with the GET type it does return JSON response.
– Martin Prusa
Aug 24 at 9:04
Yes, the API does work in POST. I know because the one who gave me the test says: "I see where the error is but you will understand that if I tell you the solution, the test would be grace since it is a problem of the code and not of the backend. .. "
– Jordi Gallen
Aug 24 at 9:37
Yeah, it seems that the API does not work with POST type, with the GET type it does return JSON response.
– Martin Prusa
Aug 24 at 9:04
Yeah, it seems that the API does not work with POST type, with the GET type it does return JSON response.
– Martin Prusa
Aug 24 at 9:04
Yes, the API does work in POST. I know because the one who gave me the test says: "I see where the error is but you will understand that if I tell you the solution, the test would be grace since it is a problem of the code and not of the backend. .. "
– Jordi Gallen
Aug 24 at 9:37
Yes, the API does work in POST. I know because the one who gave me the test says: "I see where the error is but you will understand that if I tell you the solution, the test would be grace since it is a problem of the code and not of the backend. .. "
– Jordi Gallen
Aug 24 at 9:37
add a comment |
up vote
0
down vote
If you are trying digest you will need this method :
credential = URLCredential(user: UserDefaults.standard.string(forKey: "userName") ?? "admin", password: UserDefaults.standard.string(forKey: "password") ?? "admin", persistence: .none)
let sessionMananager = Alamofire.SessionManager.default
_ = sessionMananager.request(url)
.authenticate(usingCredential: credential!)
.responseJSON { response in
}
add a comment |
up vote
0
down vote
If you are trying digest you will need this method :
credential = URLCredential(user: UserDefaults.standard.string(forKey: "userName") ?? "admin", password: UserDefaults.standard.string(forKey: "password") ?? "admin", persistence: .none)
let sessionMananager = Alamofire.SessionManager.default
_ = sessionMananager.request(url)
.authenticate(usingCredential: credential!)
.responseJSON { response in
}
add a comment |
up vote
0
down vote
up vote
0
down vote
If you are trying digest you will need this method :
credential = URLCredential(user: UserDefaults.standard.string(forKey: "userName") ?? "admin", password: UserDefaults.standard.string(forKey: "password") ?? "admin", persistence: .none)
let sessionMananager = Alamofire.SessionManager.default
_ = sessionMananager.request(url)
.authenticate(usingCredential: credential!)
.responseJSON { response in
}
If you are trying digest you will need this method :
credential = URLCredential(user: UserDefaults.standard.string(forKey: "userName") ?? "admin", password: UserDefaults.standard.string(forKey: "password") ?? "admin", persistence: .none)
let sessionMananager = Alamofire.SessionManager.default
_ = sessionMananager.request(url)
.authenticate(usingCredential: credential!)
.responseJSON { response in
}
answered Nov 19 at 16:15
ironRoei
17611
17611
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52000469%2fios-swift-4-0-basic-access-authentication-with-alamofire%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you trying to use digest protocol? if this is the issue you need to use alamo in a different way
– ironRoei
Nov 19 at 16:03