使用swift发送的JSON POST请求不被视为密钥对值,而是单个字符串

时间:2018-02-14 00:45:33

标签: swift nsmutableurlrequest

我正在发布我发送邮件请求的服务器正在接收我的数据,但它将整个帖子请求视为关键而不是多个键值对。这是处理我的数据的API代码..

API:

$postBody = $_POST ['request'];
$signature = $_POST ['signature'];
$rpcRequest = json_decode ( $postBody, true );

服务器如何看到我的数据进入(这是问题/服务器引发错误。):

array(1) {
  ["{"request":{"id":1,"method":"phoneNumberVerificationStart","params":{"number":"**********"}},"signature":"2ebdd87bdc66a04419bfd60e7c9b257039bf66dacd1623a1995c971e7cb68ed6"}"]=>
  string(0) ""
}

使用java发送相同的POST看起来像这样(这是我的代码需要生成的/服务器不会在这里抛出错误。):

array(2) {
  ["request"]=>
  array(3) {
    ["id"]=>
    int(1)
    ["method"]=>
    string(28) "phoneNumberVerificationStart"
    ["params"]=>
    array(1) {
      ["number"]=>
      string(10) "**********"
    }
  }
  ["signature"]=>
  string(64) "2ebdd87bdc66a04419bfd60e7c9b257039bf66dacd1623a1995c971e7cb68ed6"
}

我发送此请求的代码如下:

//Here I am building the request as a string. This string is used to get the signature. 
var params = 
        """
        {"method":"phoneNumberVerificationStart","params":{"number":"\(PhoneNumber)"},"id":1}
        """
//here I build the request by using a dictionary. 
var jsonParams = ["request": ["method": "phoneNumberVerificationStart","id": 1, "params": ["number": "\(PhoneNumber)"] ]] as NSMutableDictionary

let urlString = "******************************"

//below is just hashing the params into sha256
let hashedParams = sha256(request: params)
let signature = hashedParams.hexEncodedString()

//Take what was just hashed and put it into the signature variable
jsonParams["signature"] = signature

//jsonData takes my built dictionary and turns it into a json format to be sent. 
let jsonData = try? JSONSerialization.data(withJSONObject: jsonParams, options: [])

guard let requestURL = URL(string:urlString) else{return}
let session = URLSession.shared

// Set up the post request to send to the server. 
let request = NSMutableURLRequest(url:requestURL)
request.httpMethod = "POST"

// Add the jsonData to the body of the http request. This data is json and when printed out in string form it looks like this:
// ( {"request":{"id":1,"method":"phoneNumberVerificationStart","params":{"number":"**********"}},"signature":"2ebdd87bdc66a04419bfd60e7c9b257039bf66dacd1623a1995c971e7cb68ed6"}
//For some odd reason Id shifts up to the front of the json file? 
request.httpBody =  jsonData
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")


print(String(data: request.httpBody!, encoding: .utf8)!)


//After this I send the request the server does not understand my post request
let task = session.dataTask(with: request as URLRequest){
            (data,respone, error) in
            if error != nil {
                print(error?.localizedDescription)
                //print(String(data:myData!,encoding: .utf8)!)
            }
            do{

                print (String(data: data!, encoding: .utf8)!)


            }

从上面可以看出,我发送的请求不会被视为密钥对值,而是单个密钥而不是对。由于依赖它的程序,我无法编辑API代码。有没有人知道这里发生了什么?

0 个答案:

没有答案