所以我试图在Nim中编写一个Pocket API库。我有以下代码,它使用使用者密钥向袖珍服务器发出POST请求,但我不断收到400错误,指出使用者密钥丢失。
import httpclient
var client = newHttpClient()
client.headers = newHttpHeaders({ "Content-Type": "application/x-www-form-urlencoded" })
var url = "https://getpocket.com/v3/oauth/request"
let body = {
"consumer_key": "xxxxx-xxxxxxxxxxxxxxxxxxxxx",
"redirect_uri": "http://example.com/"
}
let response = client.request(url, httpMethod = HttpPost, body = $body)
echo response.status, response.headers
这是我的输出
400 Bad Request{"date": @["Thu, 18 Apr 2019 09:33:54 GMT"], "x-frame-options": @["SAMEORIGIN"], "content-length": @["15"], "p3p": @["policyref=\"/w3c/p3p.xml\", CP=\"ALL CURa ADMa DEVa OUR IND UNI COM NAV INT STA PRE\""], "x-error": @["Missing consumer key."], "content-type": @["text/html; charset=UTF-8"], "x-error-code": @["138"], "x-source": @["Pocket"], "server": @["Apache"], "status": @["400 Bad Request"], "connection": @["keep-alive"], "cache-control": @["private"]}
使用者密钥正确。我使用Postman提出了请求,得到了200条回复。
答案 0 :(得分:2)
您应该做的是导入JSON。这是一个进行JSON序列化的宏。理由是它经常使用,因此应该简短。有关更多信息,请检查check this。
如此做
client.headers = newHttpHeaders({ "Content-Type": "application/x-www-form-urlencoded" })
执行此操作
client.headers = newHttpHeaders({ "Content-Type": "application/json" })