使用Alamofire 4执行GET时设置内容类型 [英] Set content-type when performing GET using Alamofire 4
本文介绍了使用Alamofire 4执行GET时设置内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要做的就是使用Alamofire 4发出一个简单的GET请求,但将内容类型设置为application/json
All I want to do is make a simple GET request using Alamofire 4 but set the content-type to application/json
我看不到针对请求执行此操作的方法.我该怎么做.
I cannot see a way of doing it on the request. How do I do this.
我以为这样可以,但是不行.
I thought this would work but it doesn't.
Alamofire.request("https://dummy.com/dummy",
method: .get,
parameters: nil,
encoding: JSONEncoding.default,
headers: nil).responseJSON {
response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response
}
我需要这样做,以便我正在调用的服务将以JSON而不是XML的形式返回数据.有什么想法吗?
I need to do this so that the service I am calling will return the data as JSON rather than XML. Any ideas?
推荐答案
为此设置headers
,创建字典,并使用Alamofire request
将其设置为headers
.
Set headers
for that, create dictionary and set it with headers
with Alamofire request
.
let header = ["content-type" : "application/json"]
Alamofire.request("https://dummy.com/dummy",
method: .get,
parameters: nil,
encoding: JSONEncoding.default,
headers: header).responseJSON {
response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response
}
这篇关于使用Alamofire 4执行GET时设置内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文