代码之家  ›  专栏  ›  技术社区  ›  Alex Baban

用红色发出post请求时如何在头中使用变量

  •  2
  • Alex Baban  · 技术社区  · 6 年前

    我正试图用红色语言发出一个post请求。

    我必须传递带有授权的头,在发出请求并将其保存在变量中之前,我正在计算授权字符串的值 auth-string .

    probe 打印 验证字符串 值罚款。

    如果我在头部分中硬编码授权字符串,它是有效的,但是当我尝试使用变量时,程序就退出了。

    此代码带有硬编码 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
    

    如何在headers部分使用变量?

    1 回复  |  直到 6 年前
        1
  •  4
  •   Graham Chiu    6 年前

    这应该和在雷波里一样有效

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