This is my feedback json string:
{"name":"abc", "cardNumber":"1234567890", "data": [{A data},{B data}...]}
I use this function to send data, then get json and encode:
func uploadData(word:String){
var request = URLRequest(url: url!, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 30)
request.httpMethod = "POST"
request.httpBody = word.data(using: .utf8)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let session = URLSession(configuration: URLSessionConfiguration.default)
session.dataTask(with: request, completionHandler: {(data, response, error) in
if let data = data{
do{
let data = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
print(data) //I want to know what is this so I print
}catch{
print(error.localizedDescription)
}
}
}).resume()
}
But the console always says:The data couldn't be read because it isn't in the correct format
This json can be format and read in android if I use JSONObject.getJSONArray("myValue")...
I try to use print(data)(without json encode) to show if there is any data in feedback and I get 400byte in console, so I'm sure there is data send back to me.
UPDATE 12/28:
{"name":"abc",
"cardNumber":"1234567890",
"data": [{day:20171228, time: 09:10:11},
{day:20171226, time: 20:00:12},
{day:20171227, time: 15:30:22}
]
}
I'm sure this json can be read in android, the receiver and sender I use is vb.net, it use sendingString = JsonConvert.SerializeObject(JSONClass) to become json string, then convert to byte to send out.
UPDATE 12/28 new
After trying so much, I found string can get the feedback, but the value of name is Chinese word, other value is English and number, only name is unreadable, now I'm checking which String.Encoding will work, then if I encode it success, I will try to format to json Array.
Can you try this one?
func uploadData(word:String){
var request = URLRequest(url: url!, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 30)
request.httpMethod = "POST"
request.httpBody = word.data(using: .utf8)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let session = URLSession(configuration: URLSessionConfiguration.default)
session.dataTask(with: request, completionHandler: {(data, response, error) in
if let data = data{
if let returnData = String(data: data!, encoding: .utf8) {
print(returnData)
} else {
print("Invalid Data Coming")
}
do{
let data = try JSONSerialization.jsonObject(with: data, options: []) as? [String: AnyObject]
print(data) //I want to know what is this so I print
}catch{
print(error.localizedDescription)
}
}
}).resume()
}
Related
I'm trying to make an API call here using a post method, however I keep getting
[[boringssl] boringssl_metrics_log_metric_block_invoke(144)]
and the data returned is an empty object {"finalResults":[]}.
Tested the API using postman and the data returns safely.
This is my code:
var dict = Dictionary<String, String>()
dict = [
"queryText": query,
"lat": "31.206865038834433",
"long": "29.965068562105422",
"pageToken": "",
]
let url:URL = URL(string: apiEndPointURLString)!
let session = URLSession.shared
var postData = NSData()
do{
postData = try JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions.prettyPrinted) as NSData
}catch {
print("error serializing.......\n\n\n\n")
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("\(postData.length)", forHTTPHeaderField: "Content-Length")
request.setValue("text/html", forHTTPHeaderField: "Content-Type")
request.setValue("json/application", forHTTPHeaderField: "Accept")
request.httpBody = postData as Data
let task = session.dataTask(with: request as URLRequest) {
(
data, response, error) in
guard let data = data, let _:URLResponse = response, error == nil else {
print("error")
return
}
let dataString = String(data: data, encoding: String.Encoding.utf8)
print(dataString ?? "no data")
}
task.resume()
I want to access the magenta REST API in my iOS application.
Following is my code to access the API:
func getCustomerTokenusingURLSEssion(){
let url = URL(string: "HTTPURL")!
var urlRequest = URLRequest(
url: url,
cachePolicy: .reloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 10.0 * 1000)
urlRequest.httpMethod = "POST"
urlRequest.addValue("application/json", forHTTPHeaderField: "Accept")
let json1: [String: Any] = [
"username": "xyz#gmail.com",
"password":"xyz12345"]
let jsonData = try? JSONSerialization.data(withJSONObject: json1, options: .prettyPrinted)
urlRequest.httpBody = jsonData
let config = URLSessionConfiguration.default
let urlsession = URLSession(configuration: config)
let task = urlsession.dataTask(with: urlRequest){ (data, response, error) -> Void in
print("response from server: \(response)")
guard error == nil else {
print("Error while fetching remote rooms: \(error)")
return
}
guard let data = data,
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
print("Nil data received from fetchAllRooms service ")
return
}
print("JSON \(json)")
}
task.resume()
}
But I'm getting error message form the server as follow:
["message": Server cannot understand Content-Type HTTP header media type application/x-www-form-urlencoded]
Please help!
Thanks!
Here's working example of token-based authentication from iOS to magento2 using swift:
func restApiAuthorize(completionBlock: #escaping (String) -> Void) {
// Prepare json data
let json: [String: Any] = ["username": “yourusername”,
"password": “yourpassowrd”]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
// Create post request
let url = URL(string: "http://yourmagentodomain.com/index.php/rest/V1/integration/customer/token")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("\(jsonData!.count)", forHTTPHeaderField: "Content-Length")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
// Insert json data to the request
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
// 1: Check HTTP Response for successful GET request
guard let httpResponse = response as? HTTPURLResponse
else {
print("error: not a valid http response")
return
}
print(httpResponse.statusCode)
switch (httpResponse.statusCode)
{
case 200:
let responseData = String(data: data, encoding: String.Encoding.utf8)!
print ("responseData: \(responseData)")
completionBlock(responseData)
default:
print("POST request got response \(httpResponse.statusCode)")
}
}
task.resume()
}
And usage is like that:
restApiAuthorize() { (output) in
// token data, I found it important to remove quotes otherwise token contains extra quotes in the end and beginning of string
let userToken = output.replacingOccurrences(of: "\"", with: "")
print ("userToken \(userToken)")
}
you can then write your userToken to userDefaults and make feature api calls.
Best Guest you forgot to set your Content-Type, so add this:
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
I am working on a app that will take a users name and password, authenticate it and log them in. So what I am doing is taking the inputs and converting them into JSON. But before I send it they need to be in byte format for it to work.
So far this is what I have.
#IBOutlet var name: UITextField!
#IBOutlet var password: UITextField!
let json: [String: AnyObject] = ["name:": name.text! as AnyObject,
"password:": password.text! as AnyObject]
do{
let jsonData = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
//this is just an example website
let url = URL(string: "https://example.com")
let request = NSMutableURLRequest(url: url!)
request.httpMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request as URLRequest) {(data, response, error) in
guard error == nil else {
print(error)
return
}
let JSON = try! JSONSerialization.jsonObject(with: jsonData, options: .allowFragments)
print(JSON)
}
task.resume()
} catch {
print(error)
}
I know there is something called JSON marshaling but not sure if that exist in Swift.
How do I convert the JSON file into byte format in Swift?
I have some code similar to this (I've simplified it here):
let text = "abc" let iosVersion = UIDevice.currentDevice().systemVersion
let message = ["Text" : text, "IosVersion" : iosVersion]
if NSJSONSerialization.isValidJSONObject(message){
let url = NSURL(string: "http://localhost:3000/api/someapi")
var request = NSMutableURLRequest(URL: url!)
var data = NSJSONSerialization.dataWithJSONObject(message, options: nil, error: nil)
print(data)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.HTTPMethod = "POST"
request.HTTPBody = data
let task = session.dataTaskWithRequest(request, completionHandler: nil)
task.resume()
}
This works fine, but I'd like to see the JSON in a readable format so that I can copy/paste it into fiddler/curl to help diagnose my API at the server end. The println(data) line above gives me hexadecimal data. Any ideas?
Create a String from Data and it's good practice to handle the error
do {
let data = try JSONSerialization.data(withJSONObject: message)
let dataString = String(data: data, encoding: .utf8)!
print(dataString)
// do other stuff on success
} catch {
print("JSON serialization failed: ", error)
}
I'm trying to send a Patch request with a serialized JSON Body.
For some reason the server is not able to receive the body properly. I have a feeling that there seems to be a problem with the PATCH method in combination with the http request body.
let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
var URL = B2MFetcher.urlForBooking(event.unique, bookingID: booking.unique)
let request = NSMutableURLRequest(URL: URL)
request.HTTPMethod = "PATCH"
// Headers
println(token)
request.addValue(token, forHTTPHeaderField: "Authorization")
request.addValue("gzip, identity", forHTTPHeaderField: "Accept-Encoding")
// JSON Body
let bodyObject = [
"op": "cancel"
]
var jsonError: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(bodyObject, options: nil, error: &jsonError)
/* Start a new Task */
let task = session.dataTaskWithRequest(request, completionHandler: { (data : NSData!, response : NSURLResponse!, error : NSError!) -> Void in
completion(data: data, response:response , error: error)
})
task.resume()
You could try to add a Content-Type header to the request:
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
or use one of the other JSON Content-Type formats described here.
I tested it with an ExpressJS server and without the Content-Type header the server got an empty body, but with a Content-Type header it worked well.
in swift 3/4 :
let request = NSMutableURLRequest(url: NSURL(string: "http://XXX/xx/xxx/xx")! as URL)
request.httpMethod = "PATCH"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
do{
let json: [String: Any] = ["status": "test"]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
request.httpBody = jsonData
print("jsonData: ", String(data: request.httpBody!, encoding: .utf8) ?? "no body data")
} catch {
print("ERROR")
}
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(error)")
completion(false)
return
}
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("responseString = \(responseString)")
completion(true)
return
}
task.resume()
Simple Way to use patch without using HTTPBody
If you want to just use patch, you just need to change the value of the name of a specific user then it will be like:
let myurl = URL(string: "https://gorest.co.in/public-api/users/"+"\(id)?"+"name=abc")!
var request = URLRequest(url:myurl)
request.addValue("Bearer yourAuthorizationToken",forHTTPHeaderField:"Authorization")
request.httpMethod = "PATCH"
let dataTask = URLSession.shared.dataTask(with: request)
dataTask.resume()
Note: here "id" will be userId