2016-05-12 10 views
0

私はPlivo SMS APIを通じてSMSを送信しようとしています。残念ながら、リクエストHTTPメソッドが 'POST'であっても、リクエストは 'GET'として送信されます。以下の私のコードを見てください。POSTの代わりにデフォルトメソッドGETを使用するHTTPリクエスト

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) 
    } 
} 

「GET」リクエストとして投稿されたスクリーンショットをご覧ください。このisuueを解決するのを手伝ってください。 enter image description here

答えて

0

私は何か間違いを理解しました。私はURLにMessage /を入れておくべきです。

前:NSURL(文字列: "https://で"(AUTHID) ":" \(authTokenの) "@のapi.plivo.com/v1/Account/"(authId)"/Message")

訂正1:NSURL(文字列: "https://"(authId) ":" \(authToken) "@ api.plivo.com/v1/Account/"(authId)"/ メッセージ/")

最後に「/」を付けないと、「POST」の代わりに「GET」として投稿されたリクエスト 他人に役立つことを願っています。

0

私はちょっとそれを考え出したので、コードは次のようになります。

 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() 
関連する問題