search

Home  >  Q&A  >  body text

ios - Alamofire中请求参数无名称的问题

后台 Api 中单个参数的时候没有指定参数名,而 Alamofire 中的 parameters 是字典类型的。我尝试着 param = ["": "参数"] 发现是不行的。
在 AFN 中因为参数是 id 类型的,所以可以直接将一个字符串扔进去。求解 Alamofire 中怎么做 T T (后台有很多这样的 Api,安卓那边都可以,所以没必要改 Api 了。。。。)

PHP中文网PHP中文网2811 days ago1197

reply all(5)I'll reply

  • 巴扎黑

    巴扎黑2017-04-18 09:30:35

    scheme://host/path?key=value&key=value
    In the get method, the parameters you pass in are processed into the form of key=value above
    The server gets such a URL and then retrieves it according to the key Corresponding value

    In fact, when AFN processes the param of NSString, the url is scheme://host/path?null=value
    The server can handle it like this if and only if it is a single parameter. Anyway, the value obtained is what is needed and has nothing to do with the key

    1. From the above logic analysis server, there are two situations when processing url. One is as long as there is only one value, just take its value directly. Then you can try it with any key. The other is only when the key is null and the value is not null. If it is triggered next time, use param = @{[NSNull null]: param}. If it still doesn’t work, try to see how alamofire processes parameters into queries. Check the complete url to determine whether it is filtered by alamofire.
      (ps not written) A lot of the server code and alamofire are just guesses for reference only)

    reply
    0
  • 黄舟

    黄舟2017-04-18 09:30:35

    If you don’t specify a parameter name, you may just treat it as a parameter, such as processing the entire request’s uploaded parameters as one xml or json.
    Alamofire has never been used. You can check to see if there is a way to send a json string. The parameter name is also not defined.

    reply
    0
  • 阿神

    阿神2017-04-18 09:30:35

    In Alamofire's request method, parameters can only be a dictionary, which is a bit inflexible. But you can try using the following function:

    request(URLRequest: URLRequestConvertible) -> Request 

    Build an NSMutableURLRequest yourself, set its header and body, and then pass it to the request function above. When you build NSMutableURLRequest yourself, the parameters can be very flexible and do not have to be a dictionary.

    reply
    0
  • PHPz

    PHPz2017-04-18 09:30:35

    My final processing is to splice this parameter value directly after the URL, directly "path+? parameter" and then convert it to UTF8. Generally speaking, parameters passed directly are of the get type.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:30:35

    May I ask if the poster has solved it? I encountered the same problem as you. Very troubled. If you have solved the problem. Please contact me. QQ: 664071973

    reply
    0
  • Cancelreply