如何使用Julia lang HTTP库向Django RESTFUL API (DRF)发送POST请求



关于Julia Lang中的HTTP库的文档非常有限。不仅如此,而且没有关于HTTP库的最新堆栈溢出问题。

那么,如何使用Julia + HTTP向Django Restful API (DRF)发送POST请求呢?

Julia 1.7, 1.8

如果你正在发送json格式的数据(简单的Django POST请求):

begin
using JSON
using HTTP
const url = "http://127.0.0.1:8000/api/profile"
payload = Dict("email" => "email@email.com", "password" => "12345password")
response = HTTP.request(
"POST", url, ["Content-Type" => "application/json"], JSON.json(payload))
# this is necessary, JULIA discontinued python style Dictionaries
response = JSON.parse(String(response.body))
println(response)

end

如果您正在发送头信息,如身份验证令牌等

begin
using JSON
using HTTP
const url = "http://127.0.0.1:8000/api/profile"
payload = Dict("email" => "email@email.com", "password" => "12345password")
access_token = "some access token"
headers = Dict(
"Content-Type" => "application/json",
"Authorization" => "Bearer $access_token")
response = HTTP.request(
"POST", url, headers, JSON.json(payload))
# this is necessary, JULIA discontinued python style Dictionaries
response = JSON.parse(String(response.body))
println(response)

end

最新更新