mike vorisis mike vorisis - 1 month ago 15
iOS Question

How to send a request with alamofire with xml Body

I installed Alamofire in my project and now here is what I have done.

I installed postman and I put my url and inside body a xml object and I got my result.

Here is a picture of what I exactly have done with postman

enter image description here

How can I now use Alamofire or SWXMLHash to send it as I send it with postman

Thanks in advance!

EDIT

I tried this from another question:

Alamofire.request(.POST, "https://something.com" , encoding: .Custom({
(convertible, params) in
let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest

let data = (self.testString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
mutableRequest.HTTPBody = data
return (mutableRequest, nil)
}))


.responseJSON { response in


print(response.response)

print(response.result)


}
}


But it didn't send anything

This is the log:


Optional( { URL:
https://something.com } { status code: 200, headers {
Connection = "keep-alive";
"Content-Length" = 349;
"Content-Type" = "application/xml";
Date = "Wed, 02 Nov 2016 21:13:32 GMT";
Server = nginx;
"Strict-Transport-Security" = "max-age=31536000; includeSubDomains"; } })

FAILURE

Answer

Assuming you that you're missing valid HTTP headers in your request, the updated request could look like:

Alamofire.request(.POST, "https://something.com" , encoding: .Custom({
            (convertible, params) in
            let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest

            let data = (self.testString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
            mutableRequest.HTTPBody = data
            mutableRequest.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
            return (mutableRequest, nil)
        }))
    .responseJSON { response in
    print(response.response) 
    print(response.result)   
    }
}

So, basically you should add one line

mutableRequest.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")