我正在尝试使用Red语言发出POST请求。
我必须传递带有授权的标头,并且在发出请求并将其保存到变量auth-string
中之前,我正在计算授权字符串的值。
probe
可以很好地打印auth-string
值。
如果我在标头部分中对授权字符串进行了硬编码,则它可以工作,但是当我尝试使用该变量时,程序将退出。
此代码具有硬编码的 Authorization: "Basic c29tZX...
:
username: "someusernamestring"
password: "somepasswordstring"
url: to url! rejoin ["https://example.com/" username "/Account.json"]
auth-string: rejoin ["Basic " enbase/base rejoin [username ":" password] 64]
probe auth-string
response: write url [
post [
Content-Type: "application/x-www-form-urlencoded"
Authorization: "Basic c29tZXVzZXJuYW1lc3RyaW5nOnNvbWVwYXNzd29yZHN0cmluZw=="
]
{}
]
print response
但是,此代码带有变量 Authorization: auth-string
不起作用,程序退出,没有我看到的错误。
username: "someusernamestring"
password: "somepasswordstring"
url: to url! rejoin ["https://example.com/" username "/Account.json"]
auth-string: rejoin ["Basic " enbase/base rejoin [username ":" password] 64]
probe auth-string
response: write url [
post [
Content-Type: "application/x-www-form-urlencoded"
Authorization: auth-string
]
{}
]
print response
如何在标题部分使用变量?
答案 0 :(得分:4)
这应该和Rebol一样工作
response: write url compose/deep [
post [
Content-Type: "application/x-www-form-urlencoded"
Authorization: (auth-string)
]
{}
]