简体   繁体   English

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

[英]How to send POST request using Julia lang HTTP library to a Django RESTFUL API (DRF)

There is very limited documentation when it comes to the HTTP library in Julia Lang.关于 Julia Lang 中的 HTTP 库的文档非常有限。 Not just that, but there are no up to date Stack Overflow questions regarding the HTTP library in general.不仅如此,总体上还没有关于 HTTP 库的最新 Stack Overflow 问题。

With that said, how do you send POST requests using Julia + HTTP to a Django Restful API (DRF)?话虽如此,您如何使用 Julia + HTTP 将 POST 请求发送到 Django Restful API (DRF)?

Julia 1.7, 1.8 Julia 1.7, 1.8

If you are sending json formatted data (simple Django POST request):如果您要发送 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

If you are sending header information like Authentication tokens, etc.如果您要发送 header 信息,例如身份验证令牌等。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM