[SOLVED] Sending JSON data to a SQL server - cheers!

Hey guys.

I’m trying to send some JSON data to my SQL server through the frontend of my site. The code is “working” so to speak, but my server seems to dislike the information.

Here’s what I have:

local function networkListener( event ) local sendInfo = { ["email"] = "johndoe@hotmail.com", ["points"] = 3000, ["key"] = "7906145694e6035f5d51df1d0c1e0671b28a9448ecb7e7ecf0d8" } local params = {} params.body = json.encode( sendInfo ) local headers = { ["Content-Type"] = "application/json", ["Accept-Language"] = "en-US", ["Content-Length"] = #params.body } params.headers = headers network.request( "http://www.mywebsite.co.uk/index.php?", "POST", networkRequestListener, params) end

Now my web developer (who made the module to insert the data) told me to send the data in this format:

"The parameters are as following:

>> - email: string

>> - points: integer

>> - key: string (as generated in the backend)"

The data is being received, but it seems to be in the wrong format.

It’s no doubt something silly (my inexperience), but any help would be great appreciated!

Cheers

As you are receiving the correct values I would probably check on the server side if some sort of CAST/CONVERT is needed of the integer value (“points”) or maybe try to add “type” to the body sent?

I’m just guessing here a little bit but it might help you on your way.

http://spacetelescope.github.io/understanding-json-schema/reference/numeric.html

https://dev.mysql.com/doc/refman/5.7/en/charset-convert.html

Best regards,

Tomas

Thanks.

Please forgive my ignorance, but how would I apply “type” to my existing table?

Cheers

That would be on the server side. I use JSON Schema when I send a lot of information as it makes it easy and fast to catch basic errors (first-level validation) such as an integer not being an integer.

However thinking this through a little bit that should not be the problem here. I mean, if you’ll send a lot of data it would be good to use JSON Schema but it should not be necessary in your case.

What I think is that the server reads the value as a string instead of an integer just because JSON is just a string i.e. the receiving web site maybe doesn’t convert it to an integer and still reads the value as a string?

Best regards,

Tomas

Okay, I will forward this to the Web developer.

So the code looks perfectly fine? I was hoping it was something I’ve done.

Nevertheless, your input has been a big help!

I will report back if I have any further issues, or if they have been solved.

Cheers!

[“Content-Length”] = #params.body

probably isn’t doing what you think. Body isn’t a table, it’s a string since that’s what json.encode() returns. So the # operator returns a table length not a string length, so you’re setting the length to either 0 or nil.

http://www.mywebsite.co.uk/index.php?

The question mark at the end is usually associated with GET calls where the key/value pairs are included with the URL, i.e.

http://www.mywebsite.co.uk/index.php?key=value&key2=value2

You don’t need it with POST requests and probably should be left off. I don’t think that interferes with this, but its possible.

Rob

Problem solved!

It turns out that the module hasn’t been setup with JSON (no worries, communication with the Web developer on my part). So, using network.request() POST example, everything now works.

local function networkListener( event ) if ( event.isError ) then print( "Network error!" ) else print ( "RESPONSE: " ) end end local headers = {} headers["Content-Type"] = "application/x-www-form-urlencoded" headers["Accept-Language"] = "en-US" local body = "key=caa4eda4e2e891773c7a3c826a7a1501fds54ds411wbf20834571019dfa9&email=johndoe@hotmail.com&points=3000" local params = {} params.headers = headers params.body = body network.request( "http://www.randomsite.com/index.php?route=beyoti/point", "POST", networkListener, params )

As you are receiving the correct values I would probably check on the server side if some sort of CAST/CONVERT is needed of the integer value (“points”) or maybe try to add “type” to the body sent?

I’m just guessing here a little bit but it might help you on your way.

http://spacetelescope.github.io/understanding-json-schema/reference/numeric.html

https://dev.mysql.com/doc/refman/5.7/en/charset-convert.html

Best regards,

Tomas

Thanks.

Please forgive my ignorance, but how would I apply “type” to my existing table?

Cheers

That would be on the server side. I use JSON Schema when I send a lot of information as it makes it easy and fast to catch basic errors (first-level validation) such as an integer not being an integer.

However thinking this through a little bit that should not be the problem here. I mean, if you’ll send a lot of data it would be good to use JSON Schema but it should not be necessary in your case.

What I think is that the server reads the value as a string instead of an integer just because JSON is just a string i.e. the receiving web site maybe doesn’t convert it to an integer and still reads the value as a string?

Best regards,

Tomas

Okay, I will forward this to the Web developer.

So the code looks perfectly fine? I was hoping it was something I’ve done.

Nevertheless, your input has been a big help!

I will report back if I have any further issues, or if they have been solved.

Cheers!

[“Content-Length”] = #params.body

probably isn’t doing what you think. Body isn’t a table, it’s a string since that’s what json.encode() returns. So the # operator returns a table length not a string length, so you’re setting the length to either 0 or nil.

http://www.mywebsite.co.uk/index.php?

The question mark at the end is usually associated with GET calls where the key/value pairs are included with the URL, i.e.

http://www.mywebsite.co.uk/index.php?key=value&key2=value2

You don’t need it with POST requests and probably should be left off. I don’t think that interferes with this, but its possible.

Rob

Problem solved!

It turns out that the module hasn’t been setup with JSON (no worries, communication with the Web developer on my part). So, using network.request() POST example, everything now works.

local function networkListener( event ) if ( event.isError ) then print( "Network error!" ) else print ( "RESPONSE: " ) end end local headers = {} headers["Content-Type"] = "application/x-www-form-urlencoded" headers["Accept-Language"] = "en-US" local body = "key=caa4eda4e2e891773c7a3c826a7a1501fds54ds411wbf20834571019dfa9&email=johndoe@hotmail.com&points=3000" local params = {} params.headers = headers params.body = body network.request( "http://www.randomsite.com/index.php?route=beyoti/point", "POST", networkListener, params )