2016-05-12 5 views
0

Ich versuche, SMS über Plivo SMS API zu senden. Obwohl die HTTP-Methode für die Anforderung "POST" lautet, wird die Anfrage leider als "GET" gesendet. Bitte beachten Sie meinen Code unten.HTTP-Anfrage unter Verwendung der Standardmethode GET anstelle von POST

let fromNumber = "11111111111" 
    let toNumber = "111111234" 
    let message = "Hello" 

    do { 
    let json = ["src":"\(fromNumber)","dst":"\(toNumber)","text":"\(message)"] 
    let jsonData = try NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.PrettyPrinted) 
     print(jsonData) 

    // Build the request 
    let request = NSMutableURLRequest(URL: NSURL(string:"https://"\(authId)":"\(authToken)"@api.plivo.com/v1/Account/"\(authId)"/Message")!) 

    // I'm assigning the method should be 'POST' but why its going as 'GET' 

    request.HTTPMethod = "POST" 
    request.HTTPBody = jsonData 

    // Build the completion block and send the request 
     let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in 
      if error != nil{ 
       print("Error -> \(error)") 
       return 
      } 

      do { 
       let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject] 

       print("Result -> \(result)") 

      } catch { 
       print("Error -> \(error)") 
      } 
     } 

     task.resume() 
     //return task 



    } catch { 
     print(error) 
    } 
} 

Bitte schauen Sie auf den Screenshot, die Anfrage als 'GET' Anfrage geschrieben.Bitte helfen Sie mir, dieses Problem zu lösen. enter image description here

Antwort

0

Ich habe irgendwie herausgefunden, was der Fehler war. Ich hätte Message/in die URL schreiben sollen.

Vorher: NSURL (string: "https: //" (AUTHID) ":" \ (authToken) "@ api.plivo.com/v1/Account/"(authId)"/Message")

Korrekte Eins: NSURL (Zeichenfolge: "https: //" (authId) ":" \ (authToken) "@ api.plivo.com/v1/Account/"(authId)"/ Nachricht/")

Ohne "/" am Ende, Anfrage als "GET" anstelle von "POST" Hope es hilft anderen.

0

Habe ich es irgendwie aus, so sieht der Code wie folgt aus:

 let json = ["src":"Source","dst":"Destination","text":"Test SMS"] 
     let jsonData = try NSJSONSerialization.dataWithJSONObject(json, options: []) 
     print(jsonData) 

     // Build the request 
     let request = NSMutableURLRequest(URL: NSURL(string:"https://authID:[email protected]/v1/Account/authID/Message/")!) 

     request.HTTPMethod = "POST" 
     request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") 
     request.HTTPBody = jsonData 

     // Build the completion block and send the request 
     let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in 
      if error != nil{ 
       print("Error -> \(error)") 
       return 
      } 

      do { 
       let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) 

       print("Result -> \(result)") 

      } catch { 
       print("Error -> \(error)") 
      } 
     } 

     task.resume()