Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I post json data? #191

Open
zacard-orc opened this issue Jan 30, 2024 · 2 comments
Open

How can I post json data? #191

zacard-orc opened this issue Jan 30, 2024 · 2 comments

Comments

@zacard-orc
Copy link

I browsed the official document,but i can't found any info about postjson.

I try this way,but no effect,

local post_data = {
   sn = sn
}
local headers = {}
headers["Content-Type"] = "application/json;charset=utf-8"
headers["Accept"] = "application/json"

local postdata_json = json.encode(post_data)
c:setopt_httpheader(headers)
c:setopt_postfields(postdata_json)
c:setopt_writefunction(function(response_data)
   alert(response_data) 
end)

c:perform()
c:close()

can u help me ,give a sample?

@NTBBloodbath
Copy link

NTBBloodbath commented Jan 30, 2024

Hey buddy, did you try adding c:setopt_post(true) somewhere already?

@myrkraverk
Copy link

I'm sure zacard figured this out long time ago, but for anyone else coming across this and still wondering, here's an example.

local easy = curl.easy{
   url = "https://api.example.com/where/you/upload",
   httpheader = { "Api-Key: " .. api_key,
      "Content-Type: application/json" },
   postfields = json.encode( payload ),
   [ curl.OPT_VERBOSE ] = true }

easy:perform()

Of course, you don't need the verbosity for JSON to work, but I thought to leave it in the example for people still debugging. Any JSON library with an encode() function will do, or you can adjust it for your own.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants