How to Create and Send the JSON Data to Server Using Swift Language

Send Swift data via REST to a server and execute Python code

This shouldn't be too difficult. If I were you I'd ignore the iOS / Swift side at first and just get yourself a web service up and running. See http://www.dreamsyssoft.com/python-scripting-tutorial/create-simple-rest-web-service-with-python.php
This seems like a nice tutorial for a simple REST app.

For small projects, I've used sqlite and been pretty happy with it. It's relatively simple and works as advertised: https://www.tutorialspoint.com/sqlite/sqlite_python.htm

As part of your iOS / client side, include the python code that you want executed in the payload of the request. Then inside your REST app, you can execute the python code for a result, store it in a JSON object and return it to the client.

This should all be achievable within python and no need for php or anything. See this post for executing python code from python:
How do I execute a string containing Python code in Python?

Good luck

Sending JSON POST request in Swift3

Try two things:

First :

jsonData = try? JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)

then add logs if your data is converted into JSON. Convert your data into String and print the value.

Also add

request.addValue("application/json", forHTTPHeaderField: "Content-Type")

before httpBody. Sometimes we have to tell server in our request that we are posting JSON data.

Hope this will help you!!

Happy Coding!

How to POST data from multiple view controllers to server with JSON using SWIFT

I solved it by first storing them in a variable

var TITLE = UserDefaults.standard.value(forKey: "Title")
var GN = UserDefaults.standard.value(forKey: "GivenNames")
var LN = UserDefaults.standard.value(forKey: "LastName")

Then I placed them in a parameter and that's done. It was so obvious that I can't believe I didn't solve it sooner

@IBAction func save(_ sender: Any){

let parameters = ["Tax Year": TaxYear, "Title": TITLE, "first-name": GN, "sur-name": LN]

How to post a JSON with new Apple Swift Language

Nate's answer was great but I had to change the request.setvalue for it to work on my server

// create the request & response
var request = NSMutableURLRequest(URL: NSURL(string: "http://requestb.in/1ema2pl1"), cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: 5)
var response: NSURLResponse?
var error: NSError?

// create some JSON data and configure the request
let jsonString = "json=[{\"str\":\"Hello\",\"num\":1},{\"str\":\"Goodbye\",\"num\":99}]"
request.HTTPBody = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")

// send the request
NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)

// look at the response
if let httpResponse = response as? NSHTTPURLResponse {
println("HTTP response: \(httpResponse.statusCode)")
} else {
println("No HTTP response")
}

How to send JSON data using POST method swift

You can try this, may be it will help you

let request = NSMutableURLRequest(URL: NSURL(string: "Your forgetPasswordUrl")!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"

let params = ["email":validEmailTF.text] as Dictionary<String, String>

request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(params, options: [])
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
print("Response: \(response)")})

task.resume()


Related Topics



Leave a reply



Submit