gpt4 book ai didi

swift - nsMutableUrl addvalue 在 key 周围添加引号 ""

转载 作者:行者123 更新时间:2023-11-28 07:17:23 26 4
gpt4 key购买 nike

我的最终目标是一个简单的 iPhone View ,带有一个开关,当“打开”时,它会向我的 Spark Core(wifi 微芯片)发送一个 post 请求以打开继电器。

我正在努力以适用于发布请求的格式添加参数。这适用于 CLI:

curl https://api.spark.io/v1/devices/<myDeviceId>/led -d access_token=<myAccessToken> -d params=l1,LOW

这是我在 swift 中重现请求的尝试:

func toggleLight (on: Bool){
var urlToUse = sparkAPIBaseURL+coreId+lightsMethodName
var url = NSURL.URLWithString(urlToUse)
var request = NSMutableURLRequest(URL: url)
var session = NSURLSession.sharedSession()

request.HTTPMethod = "POST"

if (on==true) {
request.setValue(paramsForOn, forHTTPHeaderField: "params")
} else {
request.setValue(paramsForOff, forHTTPHeaderField: "params")
}//if

request.setValue(accessToken, forHTTPHeaderField: "access_token")

println("request: \(request)")

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

var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
println(strData)
self.responseDataLabel.text = strData
})//task

task.resume() //no idea what this does
}//toggleLight

println 显示了在键“access_token”周围使用无关引号的请求,我认为这是问题所在:

request: <NSMutableURLRequest: 0x7a62a450> { URL: https://api.spark.io/v1/devices/<myDeviceId>/led, headers: {
"access_token" = <myAccessToken>;
params = "l1,LOW"; } }

响应显示找不到访问 token 的错误:

response: <NSHTTPURLResponse: 0x7b654c60> { URL: https://api.spark.io/v1/devices/<myDeviceId>/led } { status code: 400, headers {
"Access-Control-Allow-Origin" = "*";
Connection = "keep-alive";
"Content-Length" = 104;
"Content-Type" = "application/json; charset=utf-8";
Date = "Sun, 27 Jul 2014 18:21:45 GMT";
Server = "nginx/1.6.0";
"X-Powered-By" = Express;} }{
"code": 400,
"error": "invalid_request",
"error_description": "The access token was not found"}

我尝试了 setValue 和 addValue,但它们似乎都通过添加引号和不带引号的值来处理键 access_token,其中键参数没有引号而值有。

谢谢!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~

Stack 在几个小时后不会让我回答我自己的问题,所以这里是解决问题的工作代码:谢谢 Jonah - 我从一个 friend 那里了解到 Spark Core 需要主体中的键值对,所以这是让我渡过难关的工作代码:

    func toggleLight (on: Bool){
var urlToUse = sparkAPIBaseURL+coreId+lightsMethodName
var url = NSURL.URLWithString(urlToUse)
var request = NSMutableURLRequest(URL: url)
var session = NSURLSession.sharedSession()

request.HTTPMethod = "POST"

var params: String
if (on==true) {
params = paramsForOn
} else {
params = paramsForOff
}//if

**var message = "access_token=\(accessToken)&params=\(params)"
request.HTTPBody = (message as NSString).dataUsingEncoding(NSUTF8StringEncoding)**

println("request: \(request)")

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

var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
println(strData)
self.responseDataLabel.text = strData
})//task

task.resume() //no idea what this does
}//toggleLight

最佳答案

Spark 要求您在请求正文而不是 header 中发送访问 token 。

尝试以下操作:

var message = "access_token=<token>&params=<your_params>" 

然后在请求中:

request.HTTPBody = (message as NSString).dataUsingEncoding(NSUTF8StringEncoding)

关于swift - nsMutableUrl addvalue 在 key 周围添加引号 "",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24984306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com