I get - error = "Invalid API key/secret pair.";
When I try a POST request in REST client in chrome:
I get the correct json information:
But in Swift with Alamofire I try to do the same request and get an error...
My code:
func getRawJSON(method: String) {
let publicKey = "YF9RCYRE-GL29DI0T-8GE62O2X-9OQ21A2P"
let secretKey = "79aef0ae2bb54df5c5a4e6c28757ddf54a184964fb8c978b5770895944ca7778b582ff390dffdf073a77aac1de1ea1a793dfa6629c3394465345d31a62f953e9"
let APIURL = "https://www.poloniex.com/tradingApi"
let timeNowInt = Int(NSDate().timeIntervalSince1970)
let timeNow = String(timeNowInt)
let query = NSURLComponents()
query.queryItems = [NSURLQueryItem(name: "command", value: method) as URLQueryItem,
NSURLQueryItem(name: "nonce", value: timeNow) as URLQueryItem]
let requestString = query.query!
let params = [
"command": method,
"nonce:": timeNowInt
] as [String : Any]
let codering = requestString.hmac(algorithm: .SHA512, key: secretKey)
let headers = [
"Sign": codering,
"Key": publicKey,
"Content-Type":"application/x-www-form-urlencoded"] as [String : String]
print(headers)
Alamofire.request(APIURL, withMethod: .post, parameters: params, encoding: .url, headers: headers)
.responseJSON { response in
print(response)
}
}
Related
I have a simple cURL request which inserts data into Firestore database. This works, and no authentication is needed. I need to use cURL as no Firestore library is available for watchOS.
curl -X POST -H "Content-Type: application/json" -d '
{
"fields": {
"Field1": {
"stringValue": "'"$var12"'"
},
"Field2": {
"stringValue": "'"$var22"'"
},
"Field3": {
"stringValue": "$var32"
}
}
}' "https://firestore.googleapis.com/v1/projects/project-a9c7/databases/(default)/documents/TestRuns/3001/MyRuns"
However, when I try to rewrite the request using URLRequest to use it in watchOS SwiftUI App, the app returns an error.
The previous answer did not help me.
ERROR:
The code is 404 not found, but the same URL works from terminal.
statusCode should be 2xx, but is 404
response = <NSHTTPURLResponse: 0x6000021482c0> { URL: https://firestore.googleapis.com/v1/projects/runny-a9c7/databases/(default)/documents/TestRuns/3001/MyRuns } { Status Code: 404, Headers {
"Alt-Svc" = ( ...
If I use PATCH instead of PUT as suggested, the response is 400, and the code still doesn't create new record in database.
The URLRequest call, which I tried running from SwiftUI Playground and also watchOS App:
import Foundation
// CREDIT: https://stackoverflow.com/questions/63530589/using-post-and-auth-with-firebase-database-and-swift
extension Dictionary {
func percentEncoded() -> Data? {
return map { key, value in
let escapedKey = "\(key)".addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed) ?? ""
let escapedValue = "\(value)".addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed) ?? ""
return escapedKey + "=" + escapedValue
}
.joined(separator: "&")
.data(using: .utf8)
}
func percentEncodedString() -> String? {
return map { key, value in
let escapedKey = "\(key)".addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed) ?? ""
let escapedValue = "\(value)".addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed) ?? ""
return escapedKey + "=" + escapedValue
}
.joined(separator: "&")
}
}
class Body: Codable {
var name: String
init(name: String){
self.name = name
}
}
extension CharacterSet {
static let urlQueryValueAllowed: CharacterSet = {
let generalDelimitersToEncode = ":#[]#" // does not include "?" or "/" due to RFC 3986 - Section 3.4
let subDelimitersToEncode = "!$&'()*+,;="
var allowed = CharacterSet.urlQueryAllowed
allowed.remove(charactersIn: "\(generalDelimitersToEncode)\(subDelimitersToEncode)")
return allowed
}()
}
let url = "https://firestore.googleapis.com/v1/projects/runny-a9c7/databases/(default)/documents/TestRuns/3001/MyRuns"
var request = URLRequest(url: URL(string: url)!)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "PUT"
let parameters: [String: Any] = [
"field1": "A",
"field2": "B"
]
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("App 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 = \(String(describing: responseString))")
}
task.resume()
Do you have any idea, when went wrong here, and how to fix the problem?
Thanks for any help.
I needed to use the POST method:
request.httpMethod = "POST"
Then , I adjusted the parameters:
let parameters: [String:[String:[String: String]]] = [
"fields": ["Field1": ["stringValue": "val"]]
]
Finally, I sent the JSON data using the JSONSerialization class.
request.httpBody = try? JSONSerialization.data(withJSONObject: parameters)
The data got successfully written to Firestore database.
Thanks so much Larme!.
I am trying to run a post request to send an image to the ximilar app. Not even sure if my request is 100% correct. Each time I get the same response of:
https://api.ximilar.com/recognition/v2/classify/ } { Status Code: 400, Headers {
"Content-Type" = (
"application/json"
);
Date = (
"Mon, 12 Apr 2021 04:02:58 GMT"
);
Server = (
"nginx/1.16.1"
);
"Strict-Transport-Security" = (
"max-age=31536000"
);
Vary = (
"Accept, Origin"
);
allow = (
"POST, OPTIONS"
);
"referrer-policy" = (
"same-origin"
);
"x-content-type-options" = (
nosniff
);
"x-frame-options" = (
DENY
);
} }
{
records = (
"Expected a list of items but got type \"str\"."
);
}
My code: (not sure if the body values are written right.)
let url = URL(string: "https://api.ximilar.com/recognition/v2/classify/")!
var request = URLRequest(url: url)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Token __MyTOKEN__", forHTTPHeaderField: "Authorization")
let body = [
"task_id" : "c03c288b-a249-4b17-9f63-974c2f30beb9",
"records" : "https://www.sticky.digital/wp-content/uploads/2013/11/img-6.jpg"
]
let bodyData = try? JSONSerialization.data(withJSONObject: body, options: [] )
request.httpMethod = "POST"
request.httpBody = bodyData
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()
I want to be able to output the JSON response in the console.
Here is the code in curl which works fine:
curl -H "Content-Type: application/json" -H "authorization: Token __API_TOKEN__" https://api.vize.ai/v2/classify -d '{"task_id": "0a8c8186-aee8-47c8-9eaf-348103feb14d", "version": 2, "descriptor": 0, "records": [ {"_url": "__IMAGE URL HERE__" } ] }'
The server is reporting an error for records:
Expected a list of items but got type "str"
It is saying that it was expecting an array of items for records, but only received a string. In your curl, records is an array of dictionaries with a single key, _url, but in Swift the value is just a string, consistent with what the error reported. You also supply version and descriptor in curl, but not in the Swift example.
Thus, you might try:
let body: [String: Any] = [
"task_id" : "c03c288b-a249-4b17-9f63-974c2f30beb9",
"version": 2,
"descriptor": 0,
"records" : [
[
"_url": "https://www.sticky.digital/wp-content/uploads/2013/11/img-6.jpg"
]
]
]
Alternatively, you might define Encodable types as outlined in Encoding and Decoding Custom Types:
struct XimilarRequest: Encodable {
let taskId: String
let version: Int
let descriptor: Int
let records: [XimilarRecord]
}
struct XimilarRecord: Encodable {
let url: URL
enum CodingKeys: String, CodingKey {
case url = "_url"
}
}
Then you can do:
let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
let imageUrl = URL(string: "https://www.sticky.digital/wp-content/uploads/2013/11/img-6.jpg")!
let ximilarRequest = XimilarRequest(
taskId: "c03c288b-a249-4b17-9f63-974c2f30beb9",
version: 2,
descriptor: 0,
records: [XimilarRecord(url: imageUrl)]
)
request.httpBody = try encoder.encode(ximilarRequest)
Hi here is the example that should work for you (fill the Auth token and _url of the image):
import Foundation
let headers = [ "Content-Type": "application/json", "authorization": "Token __API_TOKEN__" ] let parameters = [ "task_id": "0a8c8186-aee8-47c8-9eaf-348103feb14d", "version": 2, "records": [["_url": "__IMAGE URL HERE__"]] ] as [String : Any]
let postData = JSONSerialization.data(withJSONObject: parameters, options: [])
let request = NSMutableURLRequest(url: NSURL(string: "https://api.ximilar.com/recognition/v2/classify")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0) request.httpMethod = "POST" 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()
https://developers.google.com/youtube/v3/guides/using_resumable_upload_protocol
I try to follow this guide to start a resumable session.
But server returns this response :
success({
error = {
code = 400;
errors = (
{
domain = global;
message = "Parse Error";
reason = parseError;
}
);
message = "Parse Error";
};
})
Swift 5/ Alamofire 5
let filePath = "/Users/lucas/Documents/test.mp4"
do{
let attr = try FileManager.default.attributesOfItem(atPath: filePath)
let dict = attr as NSDictionary
let fileSize = dict.fileSize()
let dicSnippest = ["title": "TestVideo",
"description": "_description_",
"tags":"gundam",
"catagoryId":"22"]
let dicStatus = ["privacyStatus": "private",
"embedded": "True",
"license": "youtube"]
let sessionParam = ["snippet": dicSnippest,
"status": dicStatus
]
let targetUrl = "https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status"
let sessionHeaders = HTTPHeaders([
"Authorization": "Bearer \(token)",
"Content-Type": "application/json; charset=utf-8",
"Content-Length": "\(fileSize)",
"X-Upload-Content-Length": "\(fileSize)",
"X-Upload-Content-Type": "application/octet-stream"])
print("token: \(token)")
print("\(filePath): \(fileSize) bytes")
AF.request(targetUrl, method: .post, parameters: sessionParam, encoding: URLEncoding.default, headers: sessionHeaders).responseJSON {
response in
print("Http Status Code: \(String(describing: response.response?.statusCode))")
switch (response.result)
{
case .success(_):
print("Get Server Response!!")
print(response.result)
break
case .failure(let error):
print("Failure!!")
print(error)
break
}
}
} catch {
}
BTW, I modify the parameter of AF.request - encoding from URLEncoding.httpBody to JSONEncoding.default.
This error is happened:
responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)
I am trying to send the following data using Alamofire version 3 and Swift 3. And the error that I get is Extra argument method in call.
Here is what I have done so far:
struct userGoalServerConnection {
let WeightsUserGoal_POST = "http://api.umchtech.com:3000/AddWeightsGoal"
}
struct wieghtsUserGoal {
let id : Int
let token : String
let data : [ String : String ]
}
func syncInitialUserGaolValuesWithServer (userID : Int , userToken : String ){
let serverConnection = userGoalServerConnection()
let weightValue = wieghtsUserGoal.init(id: userID,
token: userToken,
data: ["weight_initial":"11","weight_end":"11","weight_difference":"11","calories_deficit":"11","difficulties":"11","weight_loss_week":"11","start_date":"2016-12-12","end_date":"2016-12-23","days_needed":"11"])
Alamofire.request(userGoalServerConnection.WeightsUserGoal_POST, method:.post, parameters: weightValue, encoding: JSONEncoding.default, headers:nil).responseJSON { response in
print(response.request as Any) // original URL request
print(response.response as Any) // URL response
print(response.result.value as Any) // result of response serialization
}
I am quite new to this so please dont mind if my question is a little bit too noob. I have gone through similar questions here but they did not help me out figuring where am i making a mistake :)
You are passing weightValue as POST param in request, it can't be. If you are using JSONEncoding then pass type of Parameters object.
let params: Parameters = [
"Key1": "value1",
"key2": "value2"
]
You can only pass dictionary type a alamofire request, you cannot post a struct type.
you can send [String : String], [String : Any] etc in a Alamofire request
You can try this way out
let WeightsUserGoal_POST = "http://api.umchtech.com:3000/AddWeightsGoal"
func Serialization(object: AnyObject) -> String{
do {
let stringData = try JSONSerialization.data(withJSONObject: object, options: [])
if let string = String(data: stringData, encoding: String.Encoding.utf8){
return string
}
}catch _ {
}
return "{\"element\":\"jsonError\"}"
}
func syncInitialUserGaolValuesWithServer (userID : Int , userToken : String ){
let data = ["weight_initial":"11","weight_end":"11","weight_difference":"11","calories_deficit":"11","difficulties":"11","weight_loss_week":"11","start_date":"2016-12-12","end_date":"2016-12-23","days_needed":"11"] as! [String : String]
let dataSerialized = Serialization(object: data as AnyObject)
let param = ["id" : userID,
"token" : userToken,
"data" : dataSerialized ]
Alamofire.request(WeightsUserGoal_POST, method: .post, parameters: param ,encoding: JSONEncoding.default, headers: nil).responseJSON { response in
print(response.request as Any) // original URL request
print(response.response as Any) // URL response
print(response.result.value as Any) // result of response serialization
}
}
I have recently upgraded to Alamofire 2.0, and now my Put request is failing with a 400 error, when it was previously working properly. I perform the call with the code:
Alamofire.request(Router.Put(query: url, params: params, encoding: .JSON))
.validate()
.responseJSON() {
(request, response, result) in
print("request: \(request)")
print("response: \(response)")
print("result: \(result)")
switch result {
case .Success(_):
// success
case .Failure(let data, _):
// error occured
}
}
and my custom Router class:
enum Router: URLRequestConvertible {
case Get(query: String, params: [String: AnyObject]?)
case Post(query: String, params: [String: AnyObject]?)
case Put(query: String, params: [String: AnyObject]?, encoding: ParameterEncoding)
case Delete(query: String, params: [String: AnyObject]?)
var URLRequest: NSMutableURLRequest {
var encodeMethod: Alamofire.ParameterEncoding = Alamofire.ParameterEncoding.URL
// Default to GET
var httpMethod: String = Alamofire.Method.GET.rawValue
let (path, parameters): (String, [String: AnyObject]?) = {
switch self {
case .Get(let query, let params):
// Set the request call
httpMethod = Alamofire.Method.GET.rawValue
// Return the query
return (query, params)
case .Post(let query, let params):
// Set the request call
httpMethod = Alamofire.Method.POST.rawValue
// Return the query
return (query, params)
case .Put(let query, let params, let encoding):
// Set the request call
httpMethod = Alamofire.Method.PUT.rawValue
// Set the encoding
encodeMethod = encoding
// Return the query
return (query, params)
case .Delete(let query, let params):
// Set the request call
httpMethod = Alamofire.Method.DELETE.rawValue
// Return the query
return (query, params)
}
}()
// Create the URL Request
let URLRequest = NSMutableURLRequest(URL: NSURL(string: Globals.BASE_URL + path)!)
// set header fields
if let key = NSUserDefaults.standardUserDefaults().stringForKey(Globals.NS_KEY_SESSION) {
URLRequest.setValue(key, forHTTPHeaderField: "X-XX-API")
}
// Add user agent
if let userAgent = NSUserDefaults.standardUserDefaults().stringForKey(Globals.NS_KEY_USER_AGENT) {
URLRequest.setValue(userAgent, forHTTPHeaderField: "User-Agent")
}
// Set the HTTP method
URLRequest.HTTPMethod = httpMethod
URLRequest.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
return encodeMethod.encode(URLRequest, parameters: parameters).0
}
}
Instead of the call being a success, the response is:
response: Optional(<NSHTTPURLResponse: 0x7fcee15b34d0> { URL: https://apiurl } { status code: 400, headers {
"Cache-Control" = "no-cache";
"Content-Length" = 26;
"Content-Type" = "application/json; charset=utf-8";
Date = "Tue, 15 Sep 2015 15:33:50 GMT";
Expires = "-1";
Pragma = "no-cache";
Server = "Microsoft-IIS/8.5";
} })
I looked into the problem and on the server side, Content-Type is coming in as blank for the request, when it should be coming in as application/json. The Content-Type should automatically get added when there is body data in the request. I have the params set:
// Save the profile
var params: [String: AnyObject] = ["indexPhoto": userProfile.indexPhoto,
"dob": df.stringFromDate(userProfile.dob) as NSString,
"identAs": userProfile.identAs]
// Add manually since creating the dictionary all at once is too much for swift to handle
params.updateValue(String(format:"%.2f", userProfile.heightIn), forKey: "heightIn")
params.updateValue(String(format:"%.2f", userProfile.weightLbs), forKey: "weightLbs")
params.updateValue(userProfile.eyes, forKey: "eyes")
params.updateValue(userProfile.hair, forKey: "hair")
...
Is there something I could be missing with this? Before I upgraded to Alamofire 2.0 this call was working just fine.
Maybe there is a different way but I fixed the same issue by sending empty params instead of nil. I don't use any parameters when using .PUT and because of that server doesn't know how to encode request and response (even if I explicitly set content type inside header, I receive blank content type on the server) so my solution was sending empty params like in the code below. Hope it helps someone.
var url = https://api.mysite.com/v1/issue/369613/delete
let params = ["":""]
let headers = NetworkConnection.addAuthorizationHeader(token, tokenType: tokenType)
manager.request(.PUT, url, parameters: params, encoding: .JSON, headers: headers)
.responseJSON { response in
if let JSONdata = response.result.value {
print("JSONdata: \(JSONdata)")
}
}