How to post JSON data to a web URL?

Hello,

I have searched everywhere and only see about reading a JSON file. I have some JSON data I need to post to a URL. How can I do this? I will be posting to an ASP.NET aspx page to read the JSON but I don’t know how in Corona.

Thanks,

Warren

I don’t know much about .NET, but with HTTP if your script supports GET requests you would just do:

network.request(“http://yourserver.com/yourscript.php?somevar={yourJSONstring}”, “GET”, networkListener)

I’m more familiar with PHP than ASP/.NET.  Your script would take the string in as a variable and in the script, you would do a json decode on that captured variable.

If you need to send via POST, you would follow the example in the network.request() doc’s that show a POST request and put your json encoded string in a variable and call network.request in post mode.

Rob

Thanks! I just needed a little direction on this. I will give it a try tomorrow and post my results.

Warren

Also, I just completed a text page in asp.net and below is some example code for returning the JSON data.

 Dim sb As New StringBuilder() Dim sw As New StringWriter(sb) Using writer As JsonWriter = New JsonTextWriter(sw) writer.Formatting = Formatting.Indented writer.WriteStartObject() writer.WritePropertyName("CPU") writer.WriteValue("Intel") writer.WritePropertyName("PSU") writer.WriteValue("500W") writer.WritePropertyName("Drives") writer.WriteStartArray() writer.WriteValue("DVD read/writer") writer.WriteComment("(broken)") writer.WriteValue("500 gigabyte hard drive") writer.WriteValue("200 gigabype hard drive") writer.WriteEnd() writer.WriteEndObject() Response.Clear() Response.ContentType = "application/json; charset=utf-8" Response.Write(sb.ToString) Response.End() End Using

And the JSON string that is returned from this when I call the URL is this:

{ "CPU": "Intel", "PSU": "500W", "Drives": ["DVD read/writer" /\*(broken)\*/, "500 gigabyte hard drive", "200 gigabype hard drive"] }

So does the JSON data look right to you? That is what I plan to read into my Corona app.

Thanks,

Warren

Try this to double-check your JSON output:   

     http://jsonlint.com/

should give useful info if there is an error.

I don’t know much about .NET, but with HTTP if your script supports GET requests you would just do:

network.request(“http://yourserver.com/yourscript.php?somevar={yourJSONstring}”, “GET”, networkListener)

I’m more familiar with PHP than ASP/.NET.  Your script would take the string in as a variable and in the script, you would do a json decode on that captured variable.

If you need to send via POST, you would follow the example in the network.request() doc’s that show a POST request and put your json encoded string in a variable and call network.request in post mode.

Rob

Thanks! I just needed a little direction on this. I will give it a try tomorrow and post my results.

Warren

Also, I just completed a text page in asp.net and below is some example code for returning the JSON data.

 Dim sb As New StringBuilder() Dim sw As New StringWriter(sb) Using writer As JsonWriter = New JsonTextWriter(sw) writer.Formatting = Formatting.Indented writer.WriteStartObject() writer.WritePropertyName("CPU") writer.WriteValue("Intel") writer.WritePropertyName("PSU") writer.WriteValue("500W") writer.WritePropertyName("Drives") writer.WriteStartArray() writer.WriteValue("DVD read/writer") writer.WriteComment("(broken)") writer.WriteValue("500 gigabyte hard drive") writer.WriteValue("200 gigabype hard drive") writer.WriteEnd() writer.WriteEndObject() Response.Clear() Response.ContentType = "application/json; charset=utf-8" Response.Write(sb.ToString) Response.End() End Using

And the JSON string that is returned from this when I call the URL is this:

{ "CPU": "Intel", "PSU": "500W", "Drives": ["DVD read/writer" /\*(broken)\*/, "500 gigabyte hard drive", "200 gigabype hard drive"] }

So does the JSON data look right to you? That is what I plan to read into my Corona app.

Thanks,

Warren

Try this to double-check your JSON output:   

     http://jsonlint.com/

should give useful info if there is an error.