如何使用xml Body发送带有alamofire的请求 [英] How to send a request with alamofire with xml Body

查看:375
本文介绍了如何使用xml Body发送带有alamofire的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中安装了Alamofire,现在这就是我所做的。



我安装了



如何我现在可以使用Alamofire或SWXMLHash发送邮件吗



提前致谢!



编辑



我试过另一个问题:

  Alamofire.request (.POST,https://something.com,参数:Dictionary(),编码:.Custom({
(可转换,params)in
let mutableRequest = convertible.URLRequest.copy() as!NSMutableURLRequest

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


.responseJSON {回复


打印(response.response)

打印(response.result)


}
}

但它没有发送任何东西



这是日志:


可选({URL:
https://something.com } {status code:200,headers {
Connection =keep-alive;
Content-Length= 349;
Content-Type=application / xml;
日期=周三,2016年11月2日21:13:32 GMT;
Server = nginx;
Strict-Transport-Security=max-age = 31536000; includeSubDomains; }})



失败


编辑



如果没有简单的添加,请不要忘记参数参数:参数:Dictionary()

解决方案

假设您在请求中缺少有效的HTTP标头,更新的请求可能如下所示:

  Alamofire。请求(.POST,https://something.com,参数:Dictionary(),编码:.Custom(
(可转换,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 {
p中的响应rint(response.response)
print(response.result)
}
}

所以,基本上你应该添加一行

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

更新

尝试相同,但使用 responseData responseString 而不是 responseJSON 因为你的回复可能不是 JSON


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

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" , parameters: Dictionary(), 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

EDIT

NEVER FORGET TO PASS parameters if you don't have simple add this , parameters: Dictionary()

解决方案

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

Alamofire.request(.POST, "https://something.com", parameters: Dictionary() , 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")

Update:
Try same, but use responseData or responseString instead of responseJSON because it is possible that your response is not JSON

这篇关于如何使用xml Body发送带有alamofire的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆