![](/img/trans.png)
[英]Unable to send POST json request to restful api using Postman
[英]How to send POST request using Julia lang HTTP library to a Django RESTFUL API (DRF)
關於 Julia Lang 中的 HTTP 庫的文檔非常有限。 不僅如此,總體上還沒有關於 HTTP 庫的最新 Stack Overflow 問題。
話雖如此,您如何使用 Julia + HTTP 將 POST 請求發送到 Django Restful API (DRF)?
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
如果您要發送 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.