在Red中发出POST请求时如何在标头中使用变量

时间:2018-07-18 23:05:25

标签: post rebol red

我正在尝试使用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

如何在标题部分使用变量?

1 个答案:

答案 0 :(得分:4)

这应该和Rebol一样工作

response: write url compose/deep [
     post [
        Content-Type: "application/x-www-form-urlencoded"
        Authorization: (auth-string)
    ]
    {}    
]