【问题标题】:Required parameter not present despite being in json encoding尽管采用 json 编码,但不存在必需的参数
【发布时间】:2018-03-31 08:14:37
【问题描述】:

我正在使用 alamofire 从 swift 发送一个 http post 请求:

let url = userActivity.webpageURL

    if(url?.description.contains(UniversalLinkHandler().urlPrefix))!
    {
        let keystore = try! EtherKeystore()
        let signedOrder = UniversalLinkHandler().parseURL(url: (url?.description)!)
        let signature = signedOrder.signature.substring(from: 2)
        let parameters: Parameters = [
            "address" : keystore.recentlyUsedWallet?.address.description,
            "indices": signedOrder.order.indices,
            "v" : signature.substring(from: 128),
            "r": signature.substring(from: 0, to: 64),
            "s": signature.substring(from: 64, to: 128)
        ]
        let query = UniversalLinkHandler.paymentServer

        Alamofire.request(
                query,
                method: .post,
                parameters: parameters
        ).responseJSON {
            result in
            print(result)
        }
    }

索引在调试器中显示为正常的 UInt16 数组,但是当发送到服务器时,它说索引数组不存在

服务器是java,使用springboot:

@RequestMapping(value = "/claimToken", method = RequestMethod.POST)
public ResponseEntity getAToken(@RequestParam(value="address") String address,
                                @RequestParam(value="indices") int[] indices,
                                @RequestParam(value = "v") byte v,
                                @RequestParam(value = "r") String r,
                                @RequestParam(value = "s") String s
)

第一个参数地址可以正常工作,但它不适用于 int[] 索引数组

非常感谢任何帮助。

【问题讨论】:

  • signedOrder.order.indices的类型是什么?
  • [UInt16] 快速数组

标签: java swift http spring-boot alamofire


【解决方案1】:

由于是您的 signedOrder.order.indices 数组导致了您的问题,并且这是 [UInt16] 类型,我的建议是:

let indices = signedOrder.order.indices.map(Int.init)
let parameters: Parameters = [
            "address" : keystore.recentlyUsedWallet?.address.description,
            "indices": indices,
            "v" : signature.substring(from: 128),
            "r": signature.substring(from: 0, to: 64),
            "s": signature.substring(from: 64, to: 128)
        ]

映射到Int 可以很好地解决您的问题。

【讨论】:

  • 不,它不起作用。此外,由于它被编码为 javascript(JSON 表示法),它是 UInt16 还是 Int 都无关紧要......不过还是谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-22
  • 2018-11-11
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
  • 2022-01-22
  • 2019-05-27
相关资源
最近更新 更多