How get body of the request from fetch of a json data #2
|
Please, provide an example for getting the body of the request when using fetch method. // request options
const options = {
method: 'POST',
body: JSON.stringify(myCustomData),
headers: {
'Content-Type': 'application/json'
}
}
// send POST request
fetch(url, options)
.then(res => res.json())
.then(res => console.log(res));I try -- display all posted values
for name,value in request:datapairs() do
print(name,'=',value)
endbut the result is empty. ECMA6 Fetch function is explained here. Thanks |
Answered by
surfskidude
Mar 14, 2022
Replies: 3 comments 1 reply
|
... trying for data in request:rawrdr() do
trace("request:rawrdr()", data)
endIt seem good |
0 replies
|
Yes, use this template code: |
0 replies
Answer selected by
Telemechanics
|
Thanks @surfskidude rawrdr() reads in block / chunk of 512 characters on Windows and 1024 on UNIX (see doc). So if json is larger, above code fails. I resolved with this, should i add error checking -- get the body of the request
local jsonstring = ""
for data in request:rawrdr() do
jsonstring = jsonstring .. data
end
trace("Check Content-Length and data length:", request:header("Content-Length"), #jsonstring)
local uploaded_json = ba.json.decode(jsonstring) |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, use this template code: