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)
end but the result is empty. ECMA6 Fetch function is explained here. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
... trying for data in request:rawrdr() do
trace("request:rawrdr()", data)
end It seem good |
Beta Was this translation helpful? Give feedback.
-
Yes, use this template code:
|
Beta Was this translation helpful? Give feedback.
-
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) |
Beta Was this translation helpful? Give feedback.
Yes, use this template code: