Having issues with a quick POST request for a token - json

So I'm trying to do a quick and dirty post request to get a token but I keep
getting this error. All I want to do for right now is print the JSON so I know I can at least test the app.
<NSHTTPURLResponse: 0x600002990600> { URL: https://development-290808.ew.r.appspot.com/token } { Status Code: 422, Headers {
"Alt-Svc" = (
"h3-29=\":443\"; ma=2592000,h3-T051=\":443\"; ma=2592000,h3-Q050=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\""
);
"Content-Length" = (
172
);
"Content-Type" = (
"application/json"
);
Date = (
"Wed, 16 Dec 2020 00:10:14 GMT"
);
Server = (
"Google Frontend"
);
"x-cloud-trace-context" = (
"a71f62144e6ce9a6c7046d700a6bad7a;o=1"
);
} }
{
detail = (
{
loc = (
body,
username
);
msg = "field required";
type = "value_error.missing";
},
{
loc = (
body,
password
);
msg = "field required";
type = "value_error.missing";
}
);
}
With this code
func getToken() {
let parameters = ["username" : "Zach", "password" : "Password"]
guard let url = URL(string: "https://development-290808.ew.r.appspot.com/token") else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else {
return }
request.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print(error)
}
}
}.resume()
}
}
And because stack overflow doesn't like it when you post mostly code and little txt, here I am adding more text so I can post this question.

Looks like it might be an issue with the request body.. your server looks to be expecting something different. Check spelling and fields on the backend.

You are using guard statement on http body, and it does not fails, but that http body is wrong, and you are not sending any params to backend, and as error says value is missing, try without guard force unwrap it(in debug purpose) and app will crash and you will get error what is wrong that part.

Related

CURL to URLRequest in Swift. (MUX LIVE STREAMING API)

Hello I am trying to create a live stream with mux api with a post request in my swift app.
This is what the cURL looks like. The MUX TOKEN ID & SECRET ID are defined in my app in swift:
curl https://api.mux.com/video/v1/live-streams \
-H "Content-Type: application/json" \
-X POST \
-d '{ "playback_policy": "public", "new_asset_settings": { "playback_policy": "public" } }' \
-u ${MUX_TOKEN_ID}:${MUX_TOKEN_SECRET}
This is my code in swift:
func getStreamKeys() {
let url = URL(string: "https://api.mux.com/video/v1/live-streams")!
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let parameters: [String: Any] = [
"playback_policy": "public",
"new_asset_settings": ["playback_policy": "public"]
]
request.httpBody = parameters.percentEncoded()
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 ?? "Unknown error")
return
}
guard (200 ... 299) ~= response.statusCode else { // check for http errors
print("statusCode should be 2xx, but is \(response.statusCode)")
print("response = \(response)")
return
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")
}
task.resume()
}
This is the response I get:
response = <NSHTTPURLResponse: 0x281c928e0> { URL: https://api.mux.com/video/v1/live-streams } { Status Code: 401, Headers {
"Cache-Control" = (
"max-age=0, private, must-revalidate"
);
"Content-Length" = (
69
);
"Content-Type" = (
"application/json; charset=utf-8"
);
Date = (
"Tue, 03 Mar 2020 22:34:45 GMT"
);
Server = (
"Mux API Server v1.89.12"
);
"x-request-id" = (
"FfjsHsz4jsEe_3oAcmYi"
);
} }
This is how the response should be:
{
"data": {
"id": "QrikEQpEXp3RvklQSHyHSYOakQkXlRId",
"stream_key": "super-secret-stream-key",
"status": "idle",
"playback_ids": [
{
"policy": "public",
"id": "OJxPwQuByldIr02VfoXDdX6Ynl01MTgC8w02"
}
],
"created_at": "1527110899"
}
}
I still need to pass -u ${MUX_TOKEN_ID}:${MUX_TOKEN_SECRET} the id and secret are constant and should be defined in the getStreamKeys. If someone can help me with it. Thanks
You'll need to add the auth to the api call as described below.
I also replaced your percentEncoded() with JSONSerialization as I do not know what you have in there.
let MUX_TOKEN_ID: String = ""
let MUX_TOKEN_SECRET: String = ""
func getStreamKeys() {
let url = URL(string: "https://api.mux.com/video/v1/live-streams")!
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let auth = "\(MUX_TOKEN_ID):\(MUX_TOKEN_SECRET)" // create string
let base64Auth = Data(auth.utf8).base64EncodedString() // base64encode the string
request.setValue("Basic \(base64Auth)", forHTTPHeaderField: "Authorization") // add auth to headers
request.httpMethod = "POST"
let parameters: [String: Any] = [
"playback_policy": "public",
"new_asset_settings": ["playback_policy": "public"]
]
do {
let jsonData = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
request.httpBody = jsonData
print(jsonData)
} catch let e {
print(e)
}
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 ?? "Unknown error")
return
}
print(response)
guard (200 ... 299) ~= response.statusCode else { // check for http errors
print("statusCode should be 2xx, but is \(response.statusCode)")
print("response = \(response)")
return
}
let responseString = String(data: data, encoding: .utf8)
}
task.resume()
}
So the -u flag on curl encodes the value as base64 and passes it as the header
Authorization: Basic <some base64 string>
So in Swift, you'd have to encode the values of ${MUX_TOKEN_ID}:${MUX_TOKEN_SECRET} then pass it into the header dictionary.
Example:
let authString = "\(MUX_TOKEN_ID):\(MUX_TOKEN_SECRET)"
let encodedAuthString = Data(authString).base64EncodedString()
request.setValue("Basic \(encodedAuthString)", forHTTPHeaderField: "Authorization")

Getting 200 response, but not showing json value

I have wrote a networking request using the "wordsapi", and I am getting a 200 status, but the response I am receiving is not in json.
My code builds, and runs I just need the response in json.
let wordsAPI =
"https://wordsapiv1.p.mashape.com/words/money/synonyms"
let wordsAPIEndPoint = NSURL(string: wordsAPI)
let request = NSMutableURLRequest(url: wordsAPIEndPoint! as
URL)
request.setValue("mapkey", forHTTPHeaderField: "X-Mashape-Key")
request.httpMethod = "GET"
let session = URLSession.shared
let synonymResponse = session.dataTask(with: request as
URLRequest) { (data, response, error) -> Void in
if let resp = response as? HTTPURLResponse {
print(resp)
}
}
synonymResponse.resume()
//response I am receiving
{ Status Code: 200, Headers {
"Accept-Ranges" = (
bytes
);
"CF-RAY" = (
"4d4d9aacabdccf5c-IAD"
);
Connection = (
"keep-alive"
);
"Content-Length" = (
30
);
"Content-Type" = (
"application/json; charset=utf-8"
);Date = (
"Fri, 10 May 2019 17:24:00 GMT"
);
I need my response to be in json for the synonyms of whatever value I set as my endpoint. In this case it is "money"
You need to use the data var in completion that contains the received json data
let wordsAPI = "https://wordsapiv1.p.mashape.com/words/money/synonyms"
let wordsAPIEndPoint = URL(string: wordsAPI)
var request = URLRequest(url: wordsAPIEndPoint!)
request.setValue("mapkey", forHTTPHeaderField: "X-Mashape-Key")
request.httpMethod = "GET"
URLSession.shared.dataTask(with: request) { (data, response, error) -> Void in
guard let data = data else { return }
let res = try! JSONDecoder().decode(Root.self,from:data)
print(res.synonyms)
}.resume()
struct Root:Codable {
let word:String
let synonyms:[String]
}

Upload image to server in swift3

I want to send an image as a parameter along with my request. I have used the code below to call my PUT request but I don't know how to append an image to the body.
func myImageUploadRequest()
{
let headers = [
"content-type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
"cache-control": "no-cache"
]
let parameters = [
[
"name": "Name",
"value": "Swift"
],
[
"name": "Key",
"fileName": "123.png"
//UIImagePNGRepresentation(myImageView.image!)
]
]
let boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW"
var body = ""
var error: NSError? = nil
do{
for param in parameters {
let paramName = param["name"]!
body += "--\(boundary)\r\n"
body += "Content-Disposition:form-data; name=\"\(paramName)\""
if let filename = param["fileName"] {
let contentType = param["content-type"]!
let fileContent = try String(contentsOfFile: filename as! String, encoding: String.Encoding.utf8)
if (error != nil) {
print(error)
}
body += "; filename=\"\(filename)\"\r\n"
body += "Content-Type: \(contentType)\r\n\r\n"
body += fileContent
} else if let paramValue = param["value"] {
body += "\r\n\r\n\(paramValue)"
}
}
// }//do
let request = NSMutableURLRequest(url: NSURL(string: "http://——----docUpload/MediaUpload/")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
request.httpMethod = "PUT"
request.allHTTPHeaderFields = headers
// request.httpBody = postData as Data
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? HTTPURLResponse
print(httpResponse)
}
})
dataTask.resume()
}
catch {
} //new
}
I have seen many examples and tried but unable to implement it myself. please check my code and help me in this.
One option is to encode the image in Base64, which you can then include as a string in your json. Although this answer refers to javascript, it explains the pluses and minuses of this approach in general.
For syntax check this answer, which lays out all possible options you might need, but the code below should provide you with the file content that you can add to the body of your request.
if let image = UIImage(named:"imageNameHere"),
let imageData = UIImagePNGRepresentation(image) {
let fileContent = imageData.base64EncodedString(options: .lineLength64Characters)
}

How to send a POST request through Swift?

I have my controller like this -
def create
if (#user = User.find_by_email(params[:email])) && #user.valid_password?(params[:password])
render json: #user.as_json(only: [:email,:authentication_token]),status: :created
else
render json:('Unauthorized Access')
end
end
When I use Postman to make this request, I choose Body, and form data and adds in the email and password. And this WORKS
How to use swift to do the same? This is what I have tried
let url = URL(string: "http://localhost:3000/api/v1/user_serialized/")
let config = URLSessionConfiguration.default
let request = NSMutableURLRequest(url: url!)
request.httpMethod = "POST"
let bodyData = "email=Test#test.com&password=Test1234"
request.httpBody = bodyData.data(using: String.Encoding.utf8);
let session = URLSession(configuration: config)
let task = session.dataTask(with: url! as URL, completionHandler: {(data, response, error) in
let json = JSON(data:data!)
debugPrint(json)
})
task.resume()
I have made a Custom HTTP class where we can sent url, parameter and we will get Data from API. Below is the class.
import Foundation
//HTTP Methods
enum HttpMethod : String {
case GET
case POST
case DELETE
case PUT
}
class HttpClientApi: NSObject{
//TODO: remove app transport security arbitary constant from info.plist file once we get API's
var request : URLRequest?
var session : URLSession?
static func instance() -> HttpClientApi{
return HttpClientApi()
}
func makeAPICall(url: String,params: Dictionary<String, Any>?, method: HttpMethod, success:#escaping ( Data? ,HTTPURLResponse? , NSError? ) -> Void, failure: #escaping ( Data? ,HTTPURLResponse? , NSError? )-> Void) {
request = URLRequest(url: URL(string: url)!)
logging.print("URL = \(url)")
if let params = params {
let jsonData = try? JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)
request?.setValue("application/json", forHTTPHeaderField: "Content-Type")
request?.httpBody = jsonData//?.base64EncodedData()
//paramString.data(using: String.Encoding.utf8)
}
request?.httpMethod = method.rawValue
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 30
configuration.timeoutIntervalForResource = 30
session = URLSession(configuration: configuration)
//session?.configuration.timeoutIntervalForResource = 5
//session?.configuration.timeoutIntervalForRequest = 5
session?.dataTask(with: request! as URLRequest) { (data, response, error) -> Void in
if let data = data {
if let response = response as? HTTPURLResponse, 200...299 ~= response.statusCode {
success(data , response , error as? NSError)
} else {
failure(data , response as? HTTPURLResponse, error as? NSError)
}
}else {
failure(data , response as? HTTPURLResponse, error as? NSError)
}
}.resume()
}
}
Now you can refer below code to get how to make an API call.
var paramsDictionary = [String:Any]()
paramsDictionary["username"] = "BBB"
paramsDictionary["password"] = "refef"
HttpClientApi.instance().makeAPICall(url: "Your URL", params:paramsDictionary, method: .POST, success: { (data, response, error) in
// API call is Successfull
}, failure: { (data, response, error) in
// API call Failure
})
I think you should pass your request instead of the url to session.dataTask
here is how my code looks like:
private let url = URL(string: "http://example.com/")!
func httpPost(jsonData: Data) {
if !jsonData.isEmpty {
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = jsonData
URLSession.shared.getAllTasks { (openTasks: [URLSessionTask]) in
NSLog("open tasks: \(openTasks)")
}
let task = URLSession.shared.dataTask(with: request, completionHandler: { (responseData: Data?, response: URLResponse?, error: Error?) in
NSLog("\(response)")
})
task.resume()
}
}
Here is the Example of POST API for calling Login API with parameters "emailaddress" and "password" with userEmailID and Userpassword as two strings holding values for email and password respectively.
You can call this POST API anywhere in your view controller, as given below:
self.postLoginCall(url: "Your post method url") example: self.postLoginCall(url: "http://1.0.0.1/api/login.php")
func postLoginCall(url : String){
let request = NSMutableURLRequest(url: NSURL(string: url)! as URL)
request.httpMethod = "POST"
let postString = "emailaddress=\(userEmailID!)&password=\(Userpassword!)"
print(postString)
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
guard error == nil && data != nil else { // check for fundamental networking error
print("error=\(error)")
return
}
do {
if let responseJSON = try JSONSerialization.jsonObject(with: data!) as? [String:AnyObject]{
print(responseJSON)
print(responseJSON["status"]!)
self.response1 = responseJSON["status"]! as! Int
print(self.response1)
//Check response from the sever
if self.response1 == 200
{
OperationQueue.main.addOperation {
//API call Successful and can perform other operatios
print("Login Successful")
}
}
else
{
OperationQueue.main.addOperation {
//API call failed and perform other operations
print("Login Failed")
}
}
}
}
catch {
print("Error -> \(error)")
}
}
task.resume()
}
Hello everyone I share below an example of a function to make a request in POST with SWIFT 5+.
This function allows you to send a POST request with an API entry point and parameters in the form of [[String: String]] and an Int to determine the output action.
For the output actions we call a function with Switch Case.
The operation is extremely simple. You have to put the two functions in one of your classes.
func MGSetRequestApi(endpoint: String, parameters: [[String: String]], MGSetAction: Int) -> String {
var setReturn: String!
let semaphore = DispatchSemaphore (value: 0)
var MGGetParam: String! = ""
for gate in parameters {
for (key, value) in gate {
let myParam = key + "=" + value + "&"
MGGetParam.append(contentsOf: myParam)
}
}
let postData = MGGetParam.data(using: .utf8)
var request = URLRequest(url: URL(string: endpoint)!,timeoutInterval: 10000)
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
setReturn = String(data: data, encoding: .utf8)!
DispatchQueue.main.async {
self.MGRequestAction(MGGetIdRq: MGSetAction, MGGetData: setReturn)
}
semaphore.signal()
}
task.resume()
semaphore.wait()
return setReturn
}
Then implement this function to manage the outputs
func MGRequestAction(MGGetIdRq: Int, MGGetData: String) {
switch MGGetIdRq {
case 1:
// Do something here
case 2:
// Do something else here
case 3:
// Do something else here again
default:
print("Set default action");
}
}
How to use this, you have two possibilities, the first one is to process what the function
MGSetRequestApi(endpoint: String, parameters: [[String: String]], MGSetAction: Int) -> String
returns (String) or to pass by the function
MGRequestAction(MGGetIdRq: Int, MGGetData: String)
which will call your Json parse function.
The MGRequestAction() function takes for parameter an Int for the choice of the action and the String of the return of the request
Now to use it do like this:
_ = MGSetRequestApi(endpoint: MY_END_POINT_API,
parameters: [["KEY_1": "VALUE 1"],
["KEY_2": "VALUE 2"],
["KEY_3": "VALUE 3"],
["KEY_4": "VALUE 4"]],
MGSetAction: 3)

URLSession request no longer accepted by server

After reviewing and trying other questions and answers on StackOverflow I continue to have the following issue.
The app is meant to verify a user's username and password, then log them into the server on another screen provided the server authenticated that the user and password are valid.
The JSON response is meant to have a key Success and if it's a 1 then ok to log on else if 0 the user can not log on.
Worked fine with Swift 2 and I performed the recommended changes moving from Swift2 to Swift 3 and have no errors but have a strange response to the following code.
let body : String = ("username=\(tkUserName.text!)&password=\(tkPassword.text!)")
var request = NSMutableURLRequest()
request = NSMutableURLRequest(url: URL(string: "https://xxxxxxxxxxxx/LoginApp")!)
request.httpMethod = "POST"
request.httpBody = body.data(using: String.Encoding.utf8)
URLSession.shared.dataTask(with: request as URLRequest)
{ (data, response, error) -> Void in
DispatchQueue.main.async(execute: { () -> Void in
if response == nil
{
//advise user no internet or other
}
else
{
var success : Int?
do {
let jsonResult = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
success = jsonResult!.object(forKey: "success") as? Int
}
catch let error as NSError
{
//action error
}
if success == 1
{
//capture all is good and go to other page
self.performSegue(withIdentifier: "unwindThenGoToTeamPage", sender: self)
}
else
{
//THIS IS WHERE IT DROPS INTO EACH TIME AS SUCCESS WAS "0"
}
}
}.resume()
Here is the server response
Event code: 3005
Event message: An unhandled exception has occurred.
Event time: 7/02/2017 10:19:31 AM
Event time (UTC): 6/02/2017 11:19:31 PM
Event ID: 1f9ff75ee64149b0994fa4a46a6ea03b
Event sequence: 5
Event occurrence: 1
Event detail code: 0
and
<NSHTTPURLResponse: 0x7b9ea440> { URL: https://xxxxxxxxxxxxx/teambeta/LoginApp } { status code: 200, headers {
"Cache-Control" = private;
"Content-Length" = 67;
"Content-Type" = "application/json; charset=utf-8";
Date = "Wed, 08 Feb 2017 03:48:09 GMT";
Server = "Microsoft-IIS/7.5";
"Set-Cookie" = "ASP.NET_SessionId=ixxxxxxxxxxx; domain=xxxxx.com; path=/; HttpOnly, .ASPXAUTH=xxxxxxxxxxx; domain=xxxxx.com; expires=Fri, 10-Mar-2017 17:08:09 GMT; path=/; HttpOnly, TIsMobileDevice=True; path=/";
"X-AspNet-Version" = "4.0.30319";
"X-AspNetMvc-Version" = "5.2";
"X-Powered-By" = "ASP.NET";
} }
There is a lot more the server responded with but the issue seems to be with the URL Session I am creating. The JSON serialization is working fine but no Success key so I am not giving the server correct URL data.
And I checked that the server is still working fine for Swift2 as well as the Android and Windows versions so I know it's my code.
Try with the following code, this is working fine at my end. Pls check:
var request = URLRequest(url:url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue(AUTENTICATION_KEY, forHTTPHeaderField: "AuthenticateKey")
request.httpBody = try! JSONSerialization.data(withJSONObject: json, options: [])
request.timeoutInterval = 30
let task = URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in
guard data != nil else
{
print("no data found: \(error)")
return
}
do
{
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary
completion(json)
}
catch let parseError
{
print(parseError)
// Log the error thrown by `JSONObjectWithData`
let jsonStr = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("Error could not parse JSON to load staff: '\(jsonStr)'")
}
})
task.resume()