簡體   English   中英

cURL和python請求之間不兼容

[英]incompatibility between cURL and python request

我有以下curl命令,該命令旨在與tplink url交互(以打開智能插頭)並按預期工作:

curl  --request POST "https://wap.tplinkcloud.com/?token=[token] HTTP/1.1" \
--data '{"method":"passthrough", "params": {"deviceId": "[deviceid]", "requestData": "{\"system\":{\"set_relay_state\":{\"state\":1}}}" }}' \
--header "Content-Type: application/json"

上面的示例按預期方式執行(我的tplink smartplug打開)。 當嘗試轉換為python請求時,我使用的是:

url = "https://wap.tplinkcloud.com?token=[token]  HTTP/1.1"

data = "{\"method\":\"passthrough\", \"params\": {\"deviceId\": \"[deviceid]\", \"requestData\": \"{\\\"system\\\":{\\\"set_relay_state\\\":{\\\"state\\\":1}}}\" }}"

headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}

r = requests.post(url, data=data, headers=headers)
print r.text

我的輸出是:

{"error_code":-20651,"msg":"Token expired"}

兩個請求都使用相同的令牌和設備ID。

我在兩個請求上都使用了httpbin.org,這是我看到的比較:

卷曲:

{
  "args": {}, 
  "data": "{\"method\":\"passthrough\", \"params\": {\"deviceId\": \"[deviceid]\", \"requestData\": \"{\\\"system\\\":{\\\"set_relay_state\\\":{\\\"state\\\":1}}}\" }}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Connection": "close", 
    "Content-Length": "160", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.54.0"
  }, 
  "json": {
    "method": "passthrough", 
    "params": {
      "deviceId": "[deviceid]", 
      "requestData": "{\"system\":{\"set_relay_state\":{\"state\":1}}}"
    }
  }, 
  "origin": "[myip]", 
  "url": "http://httpbin.org/post"
}

蟒蛇:

{
  "args": {}, 
  "data": "{\"method\":\"passthrough\", \"params\": {\"deviceId\": \"[deviceid]\", \"requestData\": \"{\\\"system\\\":{\\\"set_relay_state\\\":{\\\"state\\\":1}}}\" }}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Charset": "UTF-8", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "160", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.4"
  }, 
  "json": {
    "method": "passthrough", 
    "params": {
      "deviceId": "[deviceid]", 
      "requestData": "{\"system\":{\"set_relay_state\":{\"state\":1}}}"
    }
  }, 
  "origin": "[myip]", 
  "url": "http://httpbin.org/post"
}

我做錯了什么嗎? 也許我遇到標題問題?

是的,您缺少明顯的東西。

url = "https://wap.tplinkcloud.com?token=[token]  HTTP/1.1"

不是正確的網址。 HTTP/1.1將被解釋為令牌的一部分。

通常,我不建議使用JSON字符串文字。 在數據傳輸時,即在發出HTTP請求或寫入文件或數據庫之前,使用相應的數據結構並轉換為JSON。 至少這可以確保結構錯誤(否則在字符串中不透明)會成為編譯器錯誤。

URL參數也是如此,正確的編碼將由requests模塊透明地處理。

import requests
import json

url = "https://wap.tplinkcloud.com"
headers = {"Content-Type": "application/json", "Accept-Charset": "UTF-8"}
params = {"token": "[token]"}

request_data = {"system": {"set_relay_state":{"state":1}}}
data = {
    "method": "passthrough",
    "params": {
        "deviceId": "[deviceid]",
        "requestData": json.dumps(request_data)
    }
}

response = requests.post(url, params=params, data=json.dumps(data), headers=headers)
print response.text

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM