Chen Li Yong
Chen Li Yong

Reputation: 6097

How to set body type to JSON in Alamofire?

I'm working online with different people from different projects who take care of backend API webservice. Usually I don't have problems with sending and receiving JSON, but this time, I can't seem to be able to send JSON properly to the server.

Usually I use Alamofire to receive and send JSON message, and the usual call go like this:

let param = populateParamWithDictionary();
let url = "https://www.example.com";

Alamofire.request(.POST, url, parameters: param, headers: nil)
.responseJSON { response in {
  // take care of response here
}

But this time, I got project which the backend programmer requires me to use OAuth v2. So, let's say I've develop a function which already take care of getting the access_token string. The function now become like this:

let param = populateParamWithDictionary();
let url = "https://www.example.com";
let headers : Dictionary<String, String> = [
        "Content-Type":"application/json",
        "Authorization":"Bearer \(access_token)"
    ];

Alamofire.request(.POST, url, parameters: param, headers: headers)
.responseJSON { response in {
  // take care of response here
}

But instead of the result, I get 400 bad request error. I also even try this:

let param = populateParamWithDictionary();
let url = "https://www.example.com";
let headers : Dictionary<String, String> = [
        "Content-Type":"application/json",
        "Authorization":"Bearer \(access_token)"
    ];

Alamofire.request(.POST, url, parameters: param, encoding: ParameterEncoding.JSON, headers: headers)
.responseJSON { response in {
  // take care of response here
}

But the result is even worse. This is what I get when I print the response.

FAILURE: Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo={NSUnderlyingError=0x7fbb505788f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1017 "(null)" UserInfo={_kCFStreamErrorCodeKey=-1, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=http://lfapp.learnflux.net/v1/me, NSErrorFailingURLKey=http://lfapp.learnflux.net/v1/me, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-1, NSLocalizedDescription=cannot parse response}

But the request works if I use REST client, by setting the headers to have the authentication and Content-Type, and have the parameters to be written as plain Content, e.g. in plain API in the body content.

How can I fix this?

EDIT: The part with the access token is already clear. The access token works. I can call an API successfully if the API doesn't requires any parameters (maybe because on the server, the code doesn't bother to even check or validate the body at all because it doesn't need anything from there, hence no error raised). The problem is when I make a request which needs any parameters.

Upvotes: 1

Views: 2133

Answers (1)

Rashwan L
Rashwan L

Reputation: 38833

The error you have is probably because of encoding: ParameterEncoding.JSON in the request. Try to change it to encoding: .URLEncodedInURL. If this doesn't help you, do add your parameters to the question and if you´re make a request to get the token do the following:

if let access_token = json["access_token"]!{
     // Make the request here when you know that you have your token
}

Upvotes: 1

Related Questions