0

I have problem with parameters. This is what I need to send:

{
"id": "10",
"parameters": [
   {
  "name": "hash",
  "value": "hashText"
},
{
  "name": "search",
  "value": "text"
    }
  ] 
}

How to make correct parameter variable to this request. I use this code:

   var parameters = ["id": "10",
        "parameters": [
            "name":"hash", "value": "hash_user",
            "name":"search", "value": "text"
    ]]

    let url = NSURL(string: "http://myServerName.com/api")
    var session = NSURLSession.sharedSession()

    let request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "POST" //set http method as POST

    var err: NSError?
    request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &err)
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
        println("Response: \(response)")

   ...

Thanks for help!

EDIT:

Problem was in parameters variable. Now I have:

    var parameters = [
        "id": "10",
        "parameters": [
            ["name":"hash", "value": "hash_user"]
        ]
    ]

and it works great.

Maselko
  • 4,028
  • 2
  • 22
  • 21
  • Is it somehow possible that you can debug your server request parameter. You could first try and see if the request that you have been expecting works fine using some network debugging tools such as Postman. – Sandeep Aug 17 '15 at 08:47
  • I used Postman. I put parameters into Raw -> json and I got response. – Maselko Aug 17 '15 at 09:01
  • So, what was the error response that you got while posting from ios app. – Sandeep Aug 17 '15 at 09:28

2 Answers2

0

Change this line:

var parameters = ["id": "10",
        "parameters": [
            "name":"hash", "value": "hash_user",
            "name":"search", "value": "text"
    ]]

To this:

var parameters = ["id": "10",
        "parameters": [
            ["name":"hash", "value": "hash_user"],
            ["name":"search", "value": "text"]
    ]]

So you create array of two dictionaries in parameters

hris.to
  • 6,235
  • 3
  • 46
  • 55
  • Response: nil Body: Optional() The operation couldn’t be completed. (Cocoa error 3840.) Error could not parse JSON: 'Optional()' – Maselko Aug 17 '15 at 08:17
  • Hmm try to debug little more. First check if request.HTTPBody is properly set(as you expect). Then make sure your server is returning correct values based on your request. – hris.to Aug 17 '15 at 08:20
  • I don't know how to make it. This is not my server. Any solutions? – Maselko Aug 17 '15 at 08:25
  • Replace "json" from my answer to "parameters" from yours – SwiftStudier Aug 17 '15 at 08:50
0

Check out my answer on the same question

In case you don't have to do anything with data from server side, just remove val= from postData in those function

Community
  • 1
  • 1
SwiftStudier
  • 2,272
  • 5
  • 21
  • 43
  • I still have problem with this ;/. I put my params into list and I can't see any response. – Maselko Aug 17 '15 at 08:44
  • did you try my steps to make JSON-postData formatted correctly and my post-request function with this? – SwiftStudier Aug 17 '15 at 08:45
  • Yes i copied your code. Replace list with my params, but I have problem with variable "json". I don't know how to swap this data. – Maselko Aug 17 '15 at 08:49
  • Ok, i got something new. I use Postman in chrome. I put my json code and got response. But In app still not working. – Maselko Aug 17 '15 at 08:51
  • Check you server, because I know that this code works well. Something strange with displaying post data in your server, maybe – SwiftStudier Aug 17 '15 at 08:52