JSON POST request sent with swift is not seen as key pair values but a single string

333 Views Asked by At

I am having and issue where the server I am sending a post request to is receiving my data but it considering the entire post request as a key rather than multiple key value pairs. This is API code for how it processed my data..

API:

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

How the server sees my data coming in (This is the problem/server throws an error. ):

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

Sending the same POST with java looks like this (This is what my code needs to produce/ the server doesn't throw an error here.):

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

I am sending this request with this code:

//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)!)


            }

As can be seen above the request I am sending is getting not being seen as key pair values but rather a single key and not pairs. I am not able to edit the API code due to programs relying on it. Does anyone have an idea on what is going on here?

0

There are 0 best solutions below