How do I read message body from http response?

Hello,

I have a question.

We use network.request() to send http request.

And the listener will handle the response.

But how do I read the message body from http response?

For example, how do I read the info and player data returned from game server (maybe after login request)?

Thanks.

Have your server script output a JSON string. it will be captured in the event.response field when you get an event. Then you can convert that to a Lua table with json.decode().

local result = json.decode( event.response )

inside your network handler.

Rob

Thanks Rob.

This is what I am asking for.

But I wonder why this is not demonstrated in network.request() page…

And if I want to I send request and post my data (table or json),

How do define my header and body?

We kinda need to keep the docs somewhat generic. Not everyone wants to handle data in this model. However feel free to make a suggestion to add it. There is a link at the bottom of the page that lets you provide feedback.

Rob

Thanks Rob,

But I still have this question:

If I want to I send request and post my data (table or json),

How do define my header and body?

A lot is determined by your host script and what it expects.  But in general terms, you will have an options table that will hold a body table and header table:

local options = {} local headers = {} headers["Content-Type"] = "application/x-www-form-urlencoded" headers["Accept-Language"] = "en-US" local body = "somekey=somevalue&someotherkey=someothervalue&someadditionalkey=someadditionalvalue"   options.headers = headers options.body = body

then pass the options table to network.request. I just don’t know what your receiving script expects for it’s keys and this assumes you are sending an HTTP POST request. It’s possible that body could just be your JSON string. You will have to figure that part out on your own.

Rob

Thanks Rob.

So I can simply just put the json string in the body right?

I will mark this one solved. :smiley:

Depends on the receiving script, but most likely.

Rob

Have your server script output a JSON string. it will be captured in the event.response field when you get an event. Then you can convert that to a Lua table with json.decode().

local result = json.decode( event.response )

inside your network handler.

Rob

Thanks Rob.

This is what I am asking for.

But I wonder why this is not demonstrated in network.request() page…

And if I want to I send request and post my data (table or json),

How do define my header and body?

We kinda need to keep the docs somewhat generic. Not everyone wants to handle data in this model. However feel free to make a suggestion to add it. There is a link at the bottom of the page that lets you provide feedback.

Rob

Thanks Rob,

But I still have this question:

If I want to I send request and post my data (table or json),

How do define my header and body?

A lot is determined by your host script and what it expects.  But in general terms, you will have an options table that will hold a body table and header table:

local options = {} local headers = {} headers["Content-Type"] = "application/x-www-form-urlencoded" headers["Accept-Language"] = "en-US" local body = "somekey=somevalue&someotherkey=someothervalue&someadditionalkey=someadditionalvalue"   options.headers = headers options.body = body

then pass the options table to network.request. I just don’t know what your receiving script expects for it’s keys and this assumes you are sending an HTTP POST request. It’s possible that body could just be your JSON string. You will have to figure that part out on your own.

Rob

Thanks Rob.

So I can simply just put the json string in the body right?

I will mark this one solved. :smiley:

Depends on the receiving script, but most likely.

Rob