I am trying to use spotify's Oauth2 but when I try to get the token the response says "411 POST requests require a Content-length header. That’s all we know." but I have added the header to my post request.
#[get("/?<code>")]
fn spotify_oauth(code: String) -> String {
println!("{}", code);
let client = reqwest::Client::new();
let url = format!("https://accounts.spotify.com/api/token/?grant_type=authorization_code&code={}&redirect_uri=http://localhost:8000", code);
let res = client.post(&url)
.header(reqwest::header::Authorization("Basic MYAUTH=".to_owned()))
.header(reqwest::header::ContentType(reqwest::mime::APPLICATION_WWW_FORM_URLENCODED))
.header(reqwest::header::ContentLength(0))
.send().unwrap().text().unwrap();
res
}
Thank you.
Related
The server returning a json file that is:
{"ctrl":{"code":400,"text":"Not valid Access token","ts":"2020-03-05T11:54:01.547Z"}}
Code:
public func startDownload(url: URL, pathURL: URL) {
let accessToken: String! = "Bearer \(Constants.access_token)"
self.dirURL = pathURL
var request = URLRequest(url: url)
guard let token = accessToken else { return }
request.addValue(token, forHTTPHeaderField: "Authorization")
downloadTask = backgroundSession.downloadTask(with: request)
downloadTask.resume()
}
FYI: access token is valid, it is working with Postman.
You're going to have a problem because, unfortunatelly, there's no good solution to this issue. Authorization is one of the Reserved HTTP Headers and setting it either in URLRequest header, or in URLSessionConfiguration.httpAdditionalHeaders may simply not work:
If you set a value for one of these reserved headers, the system may ignore the value you set, or overwrite it with its own value, or simply not send it.
One might expect you could provide this token in URLSessionTaskDelegate method urlSession(_:task:didReceive:completionHandler:) which handles authentication challenges, but in there you need to provide a URLCredential object, and sadly it doesn't have a constructor that takes a Bearer token, so that's a no-go.
So basically, short of writing your own URLProtocol implementation, your best bet would be to send the token in some additional, custom, header field and have the server grab it from there (if you have control over server code). Source
How do you send an API request in Vapor 3 with the HTTPRequest struct?
I tried variations of the following code..
var headers: HTTPHeaders = .init()
let body = HTTPBody(string: a)
let httpReq = HTTPRequest(
method: .POST,
url: URL(string: "/post")!,
headers: headers,
body: body)
let httpRes: EventLoopFuture<HTTPResponse> = HTTPClient.connect(hostname: "httpbin.org", on: req).map(to: HTTPResponse.self) { client in
return client.send(httpReq)
}
The compile error Cannot convert value of type '(HTTPClient) -> EventLoopFuture<HTTPResponse>' to expected argument type '(HTTPClient) -> _'
I have tried other variations of code that worked.
Vapor 3 Beta Example Endpoint Request
let client = try req.make(Client.self)
let response: Future<Response> = client.get("http://example.vapor.codes/json")
I read and re-read:
https://api.vapor.codes/http/latest/HTTP/Structs/HTTPRequest.html
https://api.vapor.codes/http/latest/HTTP/Classes/HTTPClient.html
https://docs.vapor.codes/3.0/http/client/
Your problem is .map(to: HTTPResponse.self). Map needs to transform its result into a new result regularly, like you would map an array. However, the result of your map-closure returns an EventLoopFuture<HTTPResponse>. This results in your map function returning an EventLoopFuture<EventLoopFuture<HTTPResponse>>.
To avoid this complexity, use flatMap.
var headers: HTTPHeaders = .init()
let body = HTTPBody(string: a)
let httpReq = HTTPRequest(
method: .POST,
url: URL(string: "/post")!,
headers: headers,
body: body)
let client = HTTPClient.connect(hostname: "httpbin.org", on: req)
let httpRes = client.flatMap(to: HTTPResponse.self) { client in
return client.send(httpReq)
}
EDIT:
If you want to use the Content APIs you can do so like this:
let data = httpRes.flatMap(to: ExampleData.self) { httpResponse in
let response = Response(http: httpResponse, using: req)
return try response.content.decode(ExampleData.self)
}
HTTPClient.connect returns Future<HTTPClient> and it is mapping to a Future<HTTPResponse> not a EventLoopFuture<HTTPResponse>.
If you're expecting a single HTTPResponse use HttpClient.send instead of HTTPClient.connect.
If you're expecting multiple HTTPResponses then .map(to: HTTPResponse.self) must be changed to properly map to a EventLoopFuture<HTTPResponse>
I am developing an application in which I need to get the response code of a HTTP URL, The URL returns an xml which is encrypted. I only need to get that encrypted response code into a variable.
I've used Alamofire for http request.
Thanks in advance!
Alamofire.request("URL Goes here",method:.get)
.response { response in
if let data = response.data {
print(data)
var xml = SWXMLHash.parse(data)
print(xml["e1"]["e2"].element?.text)
}
}
I am trying to work with a MS Translator API from Swift 3 (right now playing in playgrounds, but the target platform is iOS). However, I got stuck when I was trying to get an access token for OAuth2. I have following code (I tried to port the code from example at Obtaining an access token):
let clientId = "id".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let clientSecret = "secret".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let scope = "http://api.microsofttranslator.com".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let translatorAccessURI = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"
let requestDetails = "grant_type=client_credentials&client_id=\(clientId)&client_secret=\(clientSecret)&scope=\(scope)"
let postData = requestDetails.data(using: .ascii)!
let postLength = postData.count
var request = URLRequest(url: URL(string: translatorAccessURI)!)
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("\(postLength)", forHTTPHeaderField: "Content-Length")
request.httpBody = postData
URLSession.shared.dataTask(with: webRequest) { (returnedData, response, error) in
let data = String(data: returnedData!, encoding: .ascii)
print(data)
print("**************")
print(response)
print("**************")
print(error)
}.resume()
Of course, I used a valid clientId and a valid clientSecret.
Now the callback prints following information. First, the returnedData contain a message that the request was invalid, along with a following message:
"ACS90004: The request is not properly formatted."
Second, the response comes with a 400 code (which fits the fact that the request is not properly formatted).
Third, the error is nil.
Now I was testing the call using Postman, and when I used the same URI, and put the requestDetails string as a raw body message (I added the Content-Type header manually), I got the same response. However, when I changed the body type in Postman UI to application/x-www-form-urlencoded and typed in the request details as key value pairs through its UI, the call succeeded. Now it seems that I am doing something wrong with the message formatting, or maybe even something bad with the Swift URLRequest/URLSession API, however, I cannot get a hold on to what. Can somebody help me out, please? Thanks.
OK, so after some more desperate googling and experimenting I have found my error. For the future generations:
The problem resided in encoding the parameters in the body of the PUT http request. Instead of:
let scope = "http://api.microsofttranslator.com"
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
I have to use the following:
let scope = "http://api.microsofttranslator.com"
.addingPercentEncoding(withAllowedCharacters:
CharacterSet(charactersIn: ";/?:#&=$+{}<>,").inverted)!
Seems that the API (or the HTTP protocol, I am not an expert in this) have problems with / and : characters in the request body. I have to give credit to Studiosus' answer on Polyglot issue report.
(Using iOS 9, Swift 2, Alamofire 3)
My app interacts with a REST service that requires a session token in the header. If the session token isn't already available before manager.request() is called, there's no reason to send the request only to have it fail. So I'd like to abort the request with my own error, and have the request's chained response handler take care of it - i.e. the caller wouldn't know that the request was actually never sent to the server.
What's the best way to do this with Alamofire 3?
Any way to have the same effect as Request.cancel() but with custom error, as below?:
static func request(method: Alamofire.Method, _ URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil) -> Request {
let api = apiManager.sharedInstance
guard let sessionToken = api.sessionToken else {
let req = api.alamofireManager.request(method, URLString, parameters)
req.cancel() // ***I'd like to do: req.cancelWithError(.NoSessionToken)
return req
}
// Create request and add session token to header
// ...
}