我正试图将我的HTTP请求从Python中运行到r。这是Python中的post请求:
import requests
import json
r = requests.post("https://feed-dev.ihsmarkit.com/apikey",
data={'username': 'markit/resellers/API_OPS/accounts/demo.dv', 'password':
'Example@N6'})
print("POST /apikey", r.status_code, r.reason)
apikey = r.text
print(apikey)
我做了一些研究,发现R中的http包最适合处理API相关的请求。然而,我试图在几次尝试中使用POST()函数,但得到了相同的错误400 ("MISSING_PARAMETER":参数用户名未提供。")响应。以下是我的一些尝试:
#attempt 1
response <- POST(url = "https://feed-dev.ihsmarkit.com/apikey",
add_headers(.headers = c("Content-Type"="application/x-www-form-urlencoded")),
authenticate('markit/resellers/API_OPS/accounts/demo.dv', 'Example@N6')
)
#attempt 2
request_body <- data.frame(
username = 'markit/resellers/API_OPS/accounts/demo.dv',
password = 'Example@N6'
)
request_body_json <- toJSON(list(data = request_body), auto_unbox = TRUE)
POST(url = "https://feed-dev.ihsmarkit.com/apikey",
add_headers(.headers = c("Content-Type"="application/x-www-form-urlencoded","Accept"="application/json"),
body = request_body_json))
#attempt 3
result <- POST(url = "https://feed-dev.ihsmarkit.com/apikey",
add_headers(.headers = c("Content-Type"="application/x-www-form-urlencoded","Accept"="application/json")),
body = '{"data":{"username":"markit/resellers/API_OPS/accounts/demo.dv", "password":"Example@N6}}',
encode = 'raw')
你知道我应该如何正确地转换我的请求吗?
使用
response <- POST(url = "https://feed-dev.ihsmarkit.com/apikey",
encode = "form",
body= list(
username ='markit/resellers/API_OPS/accounts/demo.dv',
password = 'Example@N6')
)
当您选择encode="form"
时,只需将您的数据作为列表传递,POST将负责将其格式化为表单数据。你的python代码似乎根本没有使用JSON。你只有字面字典值,你在那里存储你的数据。仅在HTTP端点需要基本HTTP身份验证时使用authenticate()
。对于在消息体中需要用户名/密码的端点,基本HTTP身份验证不是这样工作的。