Thanks for this lib, very cool work. You taught me how to use love2d threads and channels, LOL.
One thing I couldn't figure out though is how to POST a payload. The problem is that I can't set an ltn12 `source` argument in the `wapi.request` call's config table since ltn12 sources are functions and so can't get passed through a love2d channel.
I ended up implementing this workaround inside your code.
```
local body = {}
args.sink = ltn12.sink.table(body)
+ if args.payload then
+ args.source = ltn12.source.string(args.payload)
+ args.payload = nil
+ end
local r, c, h = http.request(args)
```
This way I can pass my request body to the request function in the `payload` property. Like so:
```
wapi.request({
method = 'POST',
url = 'https://postman-echo.com/post',
payload = '{"hello":"bello"}',
}, myCallback)
```
Is there a way to get this done without this hack?
Thanks, and a big UP! for your good work. π
Hello @andrascsibi , thanks for your message!
Indeed, you can only pass serializable objects through channels, what you did looks fine. But I must admit, I am not familiar at all with ltn12, so I don't know if there's a better way to do it π₯
As long as it works for you, it's all good π
Hello @Ulydev , great work with this lib!
I'm just trying to do a POST method, but it's giving an error and I have no idea how to do it.
I'm trying this:
'''
request = wapi.request({
method = "POST",
url = "http://142.93.253.218:5000/sendResults",
source = ltn12.source.string(reqbody)
}, function(body, headers, code)
print("code: ", code)
end)
'''
But this give me a bad argument to push. Can you help me?
Thanks and sorry about my english!
Hi @MYonemoto , it seems like you are having the same issue as described above. Basically you can't pass non-serialisable objects through channels. Could you try the workaround mentioned in the first message and let me know if that works for you? So instead of passing the source directly to `wapi.request`, you would pass `payload = reqbody`.
It worked, but my server didn't receive the body and I know my server is working because I tested it previously via postman. I will try to find out why this is happening. If you have an idea of ββwhat it might be, please tell me, I'll be thankful. Thanks for your time!
Edit:
I think I understand what's going on. For my request to work I need to pass the headers, but if I pass the headers the error "bad argument to push" happens. So, I believe the reason for the error is that headers are non-serialisable objects, right?
'''
request = wapi.request({
method = "POST",
url = "http://142.93.253.218:5000/sendResults",
payload = reqbody,
headers =
{
["Content-Type"] = "application/json",
["Content-Length"] = string.len(reqbody)
}
}, function(body, headers, code)
print(body)
end)
'''
Is there any way to resolve this? Thanks again!
You can proceed with the same method for the headers. You need to make sure your headers are passed as a string. For this you could use a JSON library to serialise the headers in your call to wapi.request then deserialise it in the thread. Let me know if you need further guidance, good luck!
If I pass my headers as a string, there is an error in http.lua, bad argument to pairs (table expected, got string).
Here's what I'm doing:
```
local headers = {
["Content-Type"] = "application/json",
["Content-Length"] = string.len(reqbody)
}
local encode = json.encode(headers)
```
When I print encode:
```
{"Content-Length":189,"Content-Type":"application/json"}
```
So:
```
request = wapi.request({
method = "POST",
url = "http://142.93.253.218:5000/sendResults",
payload = reqbody,
headers = encode
}, function(body, headers, code)
print(body)
end)
```
Am I doing wrong? If you can help me I will be very grateful, sorry for the continued inconvenience and thanks again!
You're almost there. Back in the thread code in webapi.lua, you should be able to deserialise your headers and pass the original table back using something like `args.header = json.decode(args.header)` (same place as the first comment in this issue, right after the `args.sink = ltn12.sink.table(body)` line). I understand how frustrating it can be to spend hours on a problem. If that still doesn't work, I'd be happy to help you directly if you provide a sample main.lua file.