Home > Back-end >  Getting bad request error in FedEx OAuth Swift
Getting bad request error in FedEx OAuth Swift

Time:02-15

I am trying to authorised FedEx token but getting 400 error.

FedEx Developer link : https://developer.fedex.com/api/en-us/catalog/authorization/v1/docs.html#operation/API Authorization

Code Snippet :

let headers = [
            "Content-Type": "application/x-www-form-urlencoded"
        ]
        
        let parameters = [
            "grant_type":"client_credentials",
            "client_id":"***********************",
            "client_secret":"***********************"
        ] as [String : Any]
        Alamofire.request("https://apis-sandbox.fedex.com/oauth/token", method: .post, parameters:parameters,encoding: JSONEncoding.default, headers: headers).responseJSON {
            response in
            switch response.result {
            case .success:
                print(response)
                
                break
            case .failure(let error):
                
                print(error)
            }
        }

Getting Error :

{
    errors =     (
                {
            code = "BAD.REQUEST.ERROR";
            message = "Missing or duplicate parameters. Please modify your request and try again.";
        }
    );
    transactionId = "b1d9d540-ed29-49fd-a4c2-907718e918c2";
}

CodePudding user response:

From the FedEx documentation you can see that the parameters need to be sent as form-urlencoded.

And indeed, you have specified this in your headers, but then you have used a JSON encoder, so a JSON document will be sent.

Rather, use

 Alamofire.request("https://apis-sandbox.fedex.com/oauth/token",method: .post, parameters: parameters, encoder: URLEncoding.default,  headers: headers)
  • Related