首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RxAlamofire与json主体进行post调用

RxAlamofire与json主体进行post调用
EN

Stack Overflow用户
提问于 2019-05-21 06:43:24
回答 4查看 2.1K关注 0票数 0

我想使用RxAlamofire进行post调用,但找不到任何方法。

尝试使用requestJSON方法,但没有参数来传递后置json

代码语言:javascript
复制
 RxAlamofire.requestJSON(.post, url)

如何在RxAlamofire中进行post调用并将json数据传递给post调用

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-05-26 08:59:31

使用下列代码

代码语言:javascript
复制
  var request = URLRequest(url: URL(string: "https://some_url")!)
    //Following code to pass post json 
    request.httpBody = json
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    RxAlamofire.request(request as URLRequestConvertible).responseJSON().asObservable()
票数 1
EN

Stack Overflow用户

发布于 2019-05-21 06:48:41

在适当的参数和编码条件下使用此函数

代码语言:javascript
复制
public func urlRequest(_ method: Alamofire.HTTPMethod,
                   _ url: URLConvertible,
                   parameters: [String: Any]? = nil,
                   encoding: ParameterEncoding = URLEncoding.default,
                   headers: [String: String]? = nil)
票数 0
EN

Stack Overflow用户

发布于 2022-05-07 01:22:50

amodkanthe的解决方案有效。但它的回报价值并不是很好。所以我把它改了一点

代码语言:javascript
复制
    var request = URLRequest(url: URL(string: "https://some_url")!)
    request.httpBody = jsonData  // jsonData is a Data type
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type") // this line is important
    RxAlamofire.requestJSON() //=> returns a Observable<(HttpURLResponse, Any)>, the `Any` type is actually a dictionary

附注:我使用的版本是RxAlamofire(6.1.1)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56232506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档