1

I am a beginner, trying to code to submit a vote to the Drupal7 ratings field of a node with movie content. Just as the node has a parameter for 'title', there is a parameter for 'rating-percent' which formats = "75%". A GET request of json output from a view of nodes fetches the rating percent like this (together with many other fields).

            {
        node =             {
            Body = "synopsis here";
            "rating-percent" = "75%";
            Nid = 16620;

What do I add to this code to post the data value (eg 75%) to the 'rating-percent' parameter? Here is my attempt at the POST:

// Send HTTP PUT Request
let Nid = "1"

// Define server side script URL
let ratingscriptUrl = "https://example.com/node/"

// Add one parameter
 let urlWithParams = ratingscriptUrl + Nid

// Create NSURL Ibject
 let myUrl = NSURL(string: urlWithParams);

// Create URL Request
 let request = NSMutableURLRequest(URL:myUrl!);

// Set POST request
 request1.HTTPMethod = "POST"

// Add parameters for 'ratings' field of node


// Execute HTTP Request
 let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
     data, response, error in

// Check for error
     if error != nil
  {
     print("error=\(error)")
     return
  }

Thank you so much for any help you can kindly give.

Dimitri T
  • 921
  • 1
  • 13
  • 27

1 Answers1

0

I'm using SwiftyJSON here to convert my Dictionary into JSON

let result: Dictionary<String, AnyObject> = ["Body": "synopsis", "rating-percent" :  "75%"]
let json = JSON(result).rawString()
request.HTTPBody = json!.dataUsingEncoding(NSUTF8StringEncoding)

Also you have :

// Set POST request
 request1.HTTPMethod = "POST"

But you have name your request request and not request1 but it must be a typo.

Chajmz
  • 729
  • 5
  • 11
  • Thank you Manalalz. When I wrote request it brought up an error which I posted on stackexchange and the answer/solution was that it needed to be request1.HTTPMethod = "POST". I'm a beginner so am not sure why! I will try this. Would like to clarify where in the code should it go? Is this the code to put beneath my comment to 'Add parameters' here? – Dimitri T Apr 29 '16 at 20:00
  • It was because in your previous post you did declared : `let request1: NSMutableURLRequest = NSMutableURLRequest(URL: url) ` but here you declare it as request so it must be request. For the request itself you can look at [this](http://stackoverflow.com/questions/31937686/how-to-make-http-post-request-with-json-body-in-swift) there are examples of POST request in Swift with bodies, or you can also use [Alamofire](https://github.com/Alamofire/Alamofire) it may be easier. – Chajmz Apr 29 '16 at 20:22
  • Thank you, I tried it. No errors come up in Xcode, but running it also gives no response. So I must still be missing something. – Dimitri T May 01 '16 at 01:19