Api call to .net C sharp web api project fails in swift but works fine - swift

I am using a asp.net back end with a login end point but no matter what I DO in the swift version of this code I get a 415 when I use it in .net and sharp the api works am not sure what am doing wrong here.
And yes I have enabled transport protocol but its not decoding the jwt token correctly for me in swift
Basically the end point returns the jet token used for accessing the api in an object
let jwtAccessToken: String = ""
let urlString = "http://url.com/login" *** hidden for security
purposes but is correct ****
func CallWebApi()
{
// create the url with URL
let url = URL(string: urlString)! // change server url accordingly
let parameters: [String: Any] = [ "username":
"user1#domain.com", "password": "pass1"]
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded",
forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "Post"
do {
request.httpBody = try
JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
return
}
let task = URLSession.shared.dataTask(with: request) {
data, response, error in
guard
let data = data,
let response = response as? HTTPURLResponse,
error == nil
else {
// check for fundamental networking error
print("error", error ?? URLError(.badServerResponse))
return
}
guard (200 ... 299) ~= response.statusCode else {
// check for http errors
print("statusCode should be 2xx, but is \(response.statusCode)")
print("response = \(response)")
return
}
// do whatever you want with the `data`, e.g.:
do {
let responseObject = data
print(responseObject)
} catch {
print(error)
// parsing error
if let responseString = String(data: data, encoding: .utf8) {
print("responseString = \(responseString)")
} else {
print("unable to parse response as string")
}
}
}
task.resume()
}
MyModel is basically a string
import Foundation
class AuthenticationResponse: ObservableObject {
#Published var jwtToken: String
init(jwtToken: String) {
self.jwtToken = jwtToken
}
}
I think 20 years of c sharp in not helping and am doing things it way and not the swift way if someone could advice be great.
Also in csharp we were told its not great in keeping alive the http client as can degrade performance is this the same for swift and if any library's you can recommend makes the code a bit neater the api has swagger docs enabled.
Edit 3
Example response expected back
{
"id": "b181104e-ba3e-4dba-b124-4bb4a3873b17",
"firstName": "user1",
"lastName": "lastname",
"username": "user1lastname#domainname.com",
"playerId": 0,
"jwtToken": "token in is here",//hidden for security
"error": {
"eventName": null,
"errorMessage": null,
"errorDate": null,
"statusCode": null,
"json": null
},
"refreshToken": null
}
I typically send this to the end point from C sharp
{
"username": "user1#domain.com",
"password": "pass1"
}

What I found I had to do was this
let decoder = JSONDecoder()
let responseObject = try
decoder.decode(AuthenticationResponse.self, from: data)
print(responseObject)
And change my class to be off this
import Foundation
struct AuthenticationResponse: Codable {
var jwtToken: String
}
After I done that I got the expected string back but my question is how does one get this to run correctly its completing before I think I need await but also where is it best to stored the jwttoken?

Related

How to translate a python script to swift (http post request)?

I have a working python script that gets cookies from a site. I want to convert it to Swift script, but I do something wrong and can not understand where.
#!/usr/bin/env python
import warnings
import json
import os
import requests
auth_login='someName'
auth_password='somePassword'
passport_params = {'from': 'passport', 'retpath': 'https://192.168.0.1/passport?mode=passport', 'passwd': auth_password, 'login': auth_login, 'display': 'page'}
passport_session = requests.session()
passport_error_passwd = u"wrong pass"
passport_error_login = u"no such users"
print('Connecting to passport...')
try:
passport_request = passport_session.post('https://192.168.0.1/passport?mode=auth', data=passport_params, verify=False)
except:
print('Error connecting')
if os.name == 'nt':
os.system("pause")
exit(1)
else:
print('Authentication successful')
passport_cookies = requests.utils.dict_from_cookiejar(passport_session.cookies)
print('cookies=')
print(passport_cookies)
I am not sure that my post request works correctly, so my cookies are empty.
let auth_login = "someName"
let auth_password = "somePassword"
let url = URL(string: "https://192.168.0.1/passport?mode=auth")!
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.httpMethod = "POST"
let parameters = [
"from": "passport",
"retpath": "https://192.168.0.1/passport?mode=passport",
"passwd": auth_password,
"login": auth_login,
"display": "page"
]
request.httpBody = try? JSONSerialization.data(withJSONObject: parameters)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard
// let data = data,
let response = response as? HTTPURLResponse,
let fields = response.allHeaderFields as? [String: String]
else {
print("error", error ?? URLError(.badServerResponse))
return
}
let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields, for: url)
print("cookies:\n\(cookies)")
guard (200 ... 299) ~= response.statusCode else {
print("statusCode should be 2xx, but is \(response.statusCode)")
print("response = \(response)")
return
}
}
task.resume()
Your python code is sending these:
passport_params = {'from': 'passport', 'retpath': 'https://192.168.0.1/passport?mode=passport', 'passwd': auth_password, 'login': auth_login, 'display': 'page'}
as POST data payload.
While your Swift code
let parameters = [
"from": "passport",
"retpath": "https://192.168.0.1/passport?mode=passport",
"passwd": auth_password,
"login": auth_login,
"display": "page"
]
request.httpBody = try? JSONSerialization.data(withJSONObject: parameters)
is doing that as JSON http body.
So these requests are very different, hence the problem you encounter.
Have a look at this answer how to configure POST data in a request in Swift .

Simple post request in SwiftUI

I'm beginner in SwiftUI and I'm not familiar with variable management.
I'd like to send a very simple post request like this one with SwiftUI:
let full_path : String = "https://www.example.com/get_answer?my_message=current temperature"
I've tried with this piece of code but it didn't work.
if (URL(string: full_path) != nil) {
let url = URL(string: full_path)!
var request = URLRequest(url: url)
request.httpMethod = "POST"
var decodedAnswer = String("")
do {
let task = URLSession.shared.dataTask(with: url) { data, response, error in
print(response)
decodedAnswer = String(decoding: response, as: UTF8.self)
}
}
I have the following error:
Value of optional type 'URLResponse?' must be unwrapped to a value of
type 'URLResponse'
I don't know how to get the response.
How can I get the response from a simple Post request in SwiftUI?
Multiple issues here.
You are trying to decode the URLResponse object, but what you want is the data object in the decoder.
You seem to not know about optionals. I would refer you to the basic Apple tutorials about this topic. You can find it with your favorite search engine.
You are in an async context here. Everything inside the url datasession closure will be execute after your network request returns. The code in your function will be completed by that moment and your var decodedAnswer will be out of scope. So move it out of the function in to the class/struct.
You probably want something like this:
This should be defined in class scope or you won´t be able to use it:
var decodedAnswer: String = ""
This should be in a function:
let full_path: String = "https://www.example.com/get_answer?my_message=current temperature"
if let url = URL(string: full_path) {
var request = URLRequest(url: url)
request.httpMethod = "POST"
do {
let task = URLSession.shared.dataTask(with: url) { data, response, error in
//This converts the optionals in to non optionals that could be used further on
//Be aware this will just return when something goes wrong
guard let data = data, let response = response, error == nil else{
print("Something went wrong: error: \(error?.localizedDescription ?? "unkown error")")
return
}
print(response)
decodedAnswer = String(decoding: data, as: UTF8.self)
}
task.resume()
}
}

Cloud Function post request not sending error message properly to IOS app

I have an iOS app receiving json data from a google cloud function.
The data is retrieved and works perfectly when there is no error.
However, when the cloud function returns an error, the function below recognizes error as nil
Client:
func initialize() {
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSONSerialization.data(withJSONObject: json)
let task = URLSession.shared.dataTask(with: request, completionHandler: { [weak self] (data, response, error) in
guard let response = response as? HTTPURLResponse,
response.statusCode == 200,
let data = data,
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any],
let clientSecret = json["clientSecret"] as? String,
let publishableKey = json["publishableKey"] as? String else {
///////////
//ERROR is not being recognized
/////////
let message = error?.localizedDescription ?? "Failed to decode response from server."
print("Error loading page: \(message)")
return
}
})
task.resume()
}
Client Output:
Failed to decode response from server.
Server:
firebase.functions().httpsCallable('myFunction')(message)
.then(result => {
// this works perfectly and is recognized by the swift function //
res.send({publishableKey: publishableKey, clientSecret: clientSecret});
})
.catch(err => {
// this is not recognized by the swift function //
console.log(err) // full of data
return res.status(400).send({error: err});
});
Logs(for error case):
Function execution took 240 ms, finished with status code: 400
If your requests fails I think that your error will come into the response parameter and you have to decode it. I think that the error parameter will be different than nil only if the server can't be reached or function does not exist. So basically in the else clause you will have to search into the response parameter for error.

Pass an integer as a parameter in an Alamofire PUT request

So I am trying to do a PUT request using Alamofire and I need an integer parameter (not an object).
The request sends an id to the database and the query makes an update to an object with that id in the database.
The Parameters object in alamofire seems to take only objects:
var parameters: Parameters = ["key" : "value"]
is there any way to just send an integer without using the object?
The error I keep getting when I use that method above is:
nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of int out of START_OBJECT token
and I am assuming this means I am passing an object when I should be passing an int instead.
This is my request :
Alamofire.request(url, method: .put, parameters: parameters, encoding: JSONEncoding.default, headers: nil).response{ response in
if response.response?.statusCode == 200 {
// pass
}else{
// fail
}
completionHandler((response.response?.statusCode)!)
}
I can't seem to find any examples that have to do with us online.
If you give more information about where you are sending the request, then I can test my solution to see if it works. But you can try this.
let url = "YOUR_URL"
let yourData = "WHATEVER CUSTOM VAL YOU WANT TO PASS"
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "PUT"
//request.setValue("application/json", forHTTPHeaderField: "Content-Type") //This is if you want to have headers. But it doesn't look like you need them.
request.httpBody = yourData
/*
do {
request.httpBody = try JSONSerialization.data(withJSONObject: yourData)
} catch {
print("JSON serialization failed: \(error)")
}
*/
Alamofire.request(request).responseJSON {(response) in
if response.response?.statusCode == 200 {
print ("pass")
}else{
print ("fail")
}
completionHandler((response.response?.statusCode)!)
/*
switch response.result {
case .success(let value):
print ("success: \(value)")
case .failure(let error):
print("error: \(error)")
}
*/
}

Working with Neo4j Rest API in Swift

i am quite new to iOS and Swift and now i want to use Neo4j, a Graph Database for persistent saving of my data.
I know there's a Lib named Theo for connecting to the Graph Database, but i want to use the REST API. Now i am not sure how to handle the HTTP-Request in order to get data from Database to my iOS-App written in Swift?
Anyone has some helpful clues for my?
Thanks hannes
The Theo library that you mention is a wrapper around the Neo4j REST API. The benefit of using such a library is that it takes care of things like JSON serialization, error handling, adding the correct headers to HTTP requests, etc.
You can use Cypher with the transactional Cypher REST API endpoint using Swift like this (Note: this uses the Alamofire library for handling the HTTP request.):
import Alamofire
let cypherStatement = "CREATE (:Person {name: 'Bob'})-[:LIKES]->(pizza:Food {type: 'Pizza'})<-[:LIKES]-(:Person {name: 'William'}) WITH pizza MATCH (p:Person)-[:LIKES]->(pizz) RETURN p.name AS name"
let endpoint = "http://localhost:7474/db/data/transaction/commit"
let requestBody = [
"statements": [
[
"statement": cypherStatement
]
]
]
Alamofire.request(.POST, endpoint, parameters: requestBody, encoding: .JSON)
.responseJSON { response in
debugPrint(response)
}
Which logs:
[Request]: <NSMutableURLRequest: 0x7fafcb637320> { URL: http://localhost:7474/db/data/transaction/commit }
[Response]: <NSHTTPURLResponse: 0x7fafcb81d480> { URL: http://localhost:7474/db/data/transaction/commit } { status code: 200, headers {
"Access-Control-Allow-Origin" = "*";
"Content-Length" = 91;
"Content-Type" = "application/json";
Date = "Fri, 18 Dec 2015 19:29:41 GMT";
Server = "Jetty(9.2.z-SNAPSHOT)";
} }
[Data]: 91 bytes
[Result]: SUCCESS: {
errors = (
);
results = (
{
columns = (
name
);
data = (
{
row = (
Bob
);
},
{
row = (
William
);
}
);
}
);
}
Edit
Making the same request without using Alamofire would look like this:
let cypherStatement = "CREATE (:Person {name: 'Bob'})-[:LIKES]->(pizza:Food {type: 'Pizza'})<-[:LIKES]-(:Person {name: 'William'}) WITH pizza MATCH (p:Person)-[:LIKES]->(pizz) RETURN p.name AS name"
let endpoint = "http://localhost:7474/db/data/transaction/commit"
let requestBody = [
"statements": [
[
"statement": cypherStatement
]
]
]
guard let url = NSURL(string: endpoint) else {
print("Error")
return true
}
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.addValue("application/json",forHTTPHeaderField: "Content-Type")
request.addValue("application/json",forHTTPHeaderField: "Accept")
do{
let jsonData = try NSJSONSerialization.dataWithJSONObject(requestBody, options: .PrettyPrinted)
request.HTTPBody = jsonData
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) in
let result: NSDictionary
do {
result = try NSJSONSerialization.JSONObjectWithData(data!,
options: []) as! NSDictionary
} catch {
print("error trying to convert data to JSON")
return
}
print("\(result)")
})
task.resume()
} catch let error as NSError {
print("JSON serialization error")
return true
}