2016-05-15 16 views
0

SwiftでPUTリクエストを設定しようとしています。PUTリクエストSwiftのヘッダー

Iは、以下の設定でポストマンでそれを試み、それが動作:

サーバ:

io.adafruit.com/api/feeds/ID 

ヘッダ:

x-aio-key : 52c11fca919d446ba958e43d98bdaba3 (it's not the actual key) 

体:

last_value : ON 

であります私がしたコードスウィフトに開発されたが、私は、ヘッダを送信する方法がわからない:

let url = NSURL(string: "https://io.adafruit.com/api/feeds/ID") 
     let request = NSMutableURLRequest(URL: url!) 
     request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "x-aio-key") //Optional 
     request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "52c11fca919d446ba958e43d98bdaba3") 
     request.HTTPMethod = "PUT" 
     let session = NSURLSession(configuration:NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: nil, delegateQueue: nil) 
     let data = "last_value=ON".dataUsingEncoding(NSUTF8StringEncoding) 
     request.HTTPBody = data 

     let dataTask = session.dataTaskWithRequest(request) { (data, response, error) -> Void in 

      if error != nil { 

       //handle error 
      } 
      else { 

       let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding) 
       print("Parsed JSON: '\(jsonStr)'") 
      } 
     } 
     dataTask.resume() 

答えて

1

私がお勧めすることができますがAlamofireを使用している:https://github.com/Alamofire/Alamofire

このフレームワークでは、サーバ要求を作成するのは非常に簡単です。また、リクエストに適用されるカスタムヘッダーを簡単に設定することもできます。

0

使用

request.setValue("52c11fca919d446ba958e43d98bdaba3", forHTTPHeaderField: "x-aio-key") 

代わりの

request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "x-aio-key") //Optional 
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "52c11fca919d446ba958e43d98bdaba3") 
0

私はあなたの書き込み要求の際にミスをしました。私は大胆に以下のように編集します。

例要求:

Accept: */* 
Accept-Encoding: gzip, deflate 
Content-Type: application/json 
x-aio-key: your-aio-key 
Accept-Language: en-us 

{ 
    "name": "your-new-feed-name" 
} 

スウィフト:

let url = NSURL(string: "https://io.adafruit.com/api/feeds.json") 
    let request = NSMutableURLRequest(URL: url!) 
    request.timeoutInterval = 60 

    request.setValue("your-aio-key-here", forHTTPHeaderField: "x-aio-key") 
    request.setValue("application/json", forHTTPHeaderField: "Content-Type") 
    request.setValue("gzip, deflate", forHTTPHeaderField: "Accept-Encoding") 
    request.setValue("en-us", forHTTPHeaderField: "Accept-Language") 

    request.HTTPMethod = "POST" 
    let data = "{\"name\": \"test-post-new-feed\"}".dataUsingEncoding(NSUTF8StringEncoding) 
    request.setValue("\(data!.length)", forHTTPHeaderField: "Content-Length") 
    request.HTTPBody = data 

    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() 
    let session = NSURLSession(configuration: configuration) 


    let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in 
     dispatch_async(dispatch_get_main_queue(), { 
      print("Response: \(response)") 

      if ((error) != nil) { 
       print(error) 
      } else { 
       let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding) 
       print("Parsed JSON: '\(jsonStr)'") 
      } 
     }) 
    }) 
    task.resume() 

応答:

HTTP/1.1 201 Created 
Content-Type: application/json 
Location: http://localhost:3002/api/feeds/22 
Connection: close 
Cache-Control: max-age=0, private, must-revalidate 
Etag: "7215ee9c7d9dc229d2921a40e899ec5f" 

解析されたJSON:

{ 
    "id":id-of-new-feed-post, 
    "key":"test-post-new-feed", 
    "name":"test-post-new-feed", 
    "description":null, 
    "history":true, 
    "unit_type":null, 
    "unit_symbol":null, 
    "last_value":null, 
    "status":"online", 
    "visibility":"private", 
    "enabled":true, 
    "license":null, 
    "created_at":"2016-05-16T03:44:41.402Z", 
    "updated_at":"2016-05-16T03:44:45.404Z" 
} 

より:

*** Data formats: json, form-encoded, csv 

*** Required Fields: name - string, alphanumeric, unique per user. 

*** Optional: description - text; unit_type - string; unit_symbol - string; visibility - {public, private} 

*** Response Codes: 201: Successful, includes location header to the new feed. 

結果: result image here - click to view

関連する問題