0

I working on an iOS app, that I use "Alamofire" for make API request, I test API(which created with Slim framework) by postman, but I couldn't get equivalent request of postman by Alamofire.

Postman request as below: enter image description here

and I try Alamofire as below:

  let parm = ["Password_Father": "test", "Username_Father": "test"]
        let headers :  HTTPHeaders = ["content-type": "application/json"]
        AF.request(URL(string: "http://my.domain.com/page1/login")!,
                   method: .get, // also try it as post but get error 500 as postman when using post method
                   parameters:parm,
                   encoding: JSONEncoding.default,
                   headers: headers)
            .validate(statusCode: 200..<300)
            .responseJSON { response in
                switch response.result
                {
                case .success(_) :do {
                    print("success")
                    }
                case .failure(let error):
                    print("failure(error)",error)

                    break
                }
        }

but I get this error failure(error) urlRequestValidationFailed(reason: Alamofire.AFError.URLRequestValidationFailureReason.bodyDataInGETRequest)

How can I get equivalent request of postman by Alamofire.

Ali A. Jalil
  • 873
  • 11
  • 25
  • maybe some headers are missing? In postman the tab shows `(9)`, yet you only have one in the code? – Clashsoft Apr 19 '20 at 23:12
  • https://stackoverflow.com/questions/60960976/swift-5-alamofire-5-get-method-error-alamofire-aferror-urlrequestvalidation : You need to reach out to the API provider and request a change, as no device running Apple's 2019 OSes will be able to make such a request. – Manav Apr 19 '20 at 23:33
  • @clashsoft I not put anything at header parameters, but postman put something such as caching. – Ali A. Jalil Apr 19 '20 at 23:41
  • I guest that it's `https` vs `http` issue. `http` needs some configuration in Info.plist. – AechoLiu Apr 20 '20 at 02:56

2 Answers2

0

After google and found this.


Adding a Domain Exception

Adding a domain exception is easy. You add the NSExceptionDomains key to the NSAppTransportSecurity dictionary of the target's Info.plist. The value of the key is a dictionary with every key of the dictionary being a domain exception. Take a look at the following example for clarification.

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <false/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>cocoacasts.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
   </dict>
</dict>

In above example, it add a sub-domain cocoacasts.com, and set NSExceptionAllowsInsecureHTTPLoads to allow http request for that sub-domain. The post said it at beginning.

Apple recently announced that every build submitted to the App Store needs to have App Transport Security enabled starting on 1 January 2017.

It means all communications run https by default. If the app need http, then the Info.plist needs to be configured for those exception sub-domains.

AechoLiu
  • 17,522
  • 9
  • 100
  • 118
0

As you have stated in your question you are making a "GET" request.

The GET requests cannot have a message body. But you still can send data to the server using the URL parameters. In this case, you are limited to the maximum size of the URL, which is about 2000 characters.

One way to solve this is to use URLEncoding.default as encoding in the request.

your request should be as follow:

let parm = ["Password_Father": "test", "Username_Father": "test"]
        let headers :  HTTPHeaders = ["content-type": "application/json"]
        AF.request(URL(string: "http://my.domain.com/page1/login")!,
                   method: .get,
                   parameters:parm,
                   encoding: URLEncoding.default,
                   headers: headers)
            .validate(statusCode: 200..<300)
            .responseJSON { response in
                switch response.result
                {
                case .success(_) :do {
                    print("success")
                    }
                case .failure(let error):
                    print("failure(error)",error)

                    break
                }
        }

This should work. If you encounter an issue, please feel free to comment.

kuldip
  • 46
  • 6