Why is my JSON which contains HTML causing errors? - asp.net-mvc

My JSON that is being returned from my ASP.NET MVC application looks like this:
{code: "1", error: "0", output: "<div class="a1"><div class="b1">this is fasta's</div></div>}
It is not working because I am not escaping it properly.
I'm not using a JSON library, can someone recommend a function that would clean up my HTML so this works?
I tried escaping for \ but it still doesn't work.
If I HTML encode it, should it work fine?
There will be user generated content so this has to work for all potential inputs by the user.

Are you able to use single quotes to wrap your HTML attribute values? I think that should work if you are able to do that.
For example,
{code: "1", error: "0", output: "<div class='a1'><div class='b1'>this is fasta's</div></div>"}
If that doesn't work, try using 2 backslashes to escape the double quotes.
For example,
{code: "1", error: "0", output: "<div class=\\"a1\\"><div class=\\"b1\\">this is fasta's</div></div>"}

You need to escape the internal double quotes, like this:
{code: "1", error: "0", output: "<div class=\"a1\"><div class=\"b1\">this is fasta's</div></div>"}

Related

Rails receive params as string

I'm building an API with Ruby on Rails. Because of that, I can't force the params to follow sone restriction.
Currently, I'm having an error with params that contained a Windows newline, because the newline isn't escaped.
The request body looks like below:
{
"name": "Jon Do",
"address": "6th Street\r\nDistrict City\r\nNation"
}
And it generated JSON::ParserError: 784: unexpected token
How to correctly process that request so params[:address] can be called without error?
When passing the data from front-end, make it JSON.stringify, and parse from back-end.
#Front-end
address: JSON.stringify(objects)
#Back-end
JSON.parse(params[:address])

How can i parse this json output?

["from": /topics/nursery-a, "is_background": false, "flag": 1, "data": {"msg_head_id":{"msg_head_id":7,"name":"Section","created_at":"2017-01-24 19:34:43"},"msg_title":"hello","message":{"msg_head_id":7,"doc_url":"","msg_title":"hello","created_at":"2017-02-24 10:11:07","message_id":225,"message":"welcome"},"user":{"user_id":57,"gcm_registration_id":"","name":"Iron","created_at":"2016-12-27 12:41:18","email":"info#mail.in"}}, "title": Smart , "collapse_key": do_not_collapse]
it's not a valid JSON. it should be start and close with { and }, remove [ and ], also quotation mark is missing. After these changes try this in http://json.parser.online.fr/
According to the official site, json is built on two structures -- which named dictionary and array in iOS.
So this json output isn't valid json. The value of a json key should be one of array/object(key value pairs)/number/string/boolean/null.
The from, title, collapse_key should boxed in "" to be a string.
And the json output should start and end with {} not [].

Rails 4 API, how to create a URL from a JSON response?

I'm working on an API where I have a few routes setup, ie
http://localhost:3000/phone_number_lookup/1234567890
which can return a JSON response like so:
{
"AccountCode": "1234",
"AccountID": 13579,
"BalanceCurrent": "5000",
"Phone": "1234567890",
"Id": 123123,
"SerialNumber": "Y2K2000XY2016",
"MACADDRESS": "y2k2000xy2016",
"EQUIPMENTTYPE_Name": "Motorola DCX100 HD DVR",
"ADDRESS_Zip": "90210",
"ItemID": 12345,
"iVideoSystemID": 1000001
"id": null
}
The next 'step' of the API consumption would be, 'given the initially returned response, use 4 of those parameters and pass them into a remote URL that will then do something.'
Like so:
http://myremoteURL.com/Service/?Param1=sSerialNumber&Param2=iVideoSystemID&Param3=sMAC&Param4=ItemID
It would be one thing to just set up a route that takes 4 parameters, but the route needs to be contingent on what the initial JSON response was.
What is the proper way to do this?
First of all, you'll have to convert your JSON to hash. Something like this will do:
[7] pry(main)> hash=JSON.parse(json)
=> {"AccountCode"=>"1234",
"AccountID"=>13579,
"BalanceCurrent"=>"5000",
"Phone"=>"1234567890",
"Id"=>123123,
"SerialNumber"=>"Y2K2000XY2016",
"MACADDRESS"=>"y2k2000xy2016",
"EQUIPMENTTYPE_Name"=>"Motorola DCX100 HD DVR",
"ADDRESS_Zip"=>"90210",
"ItemID"=>12345,
"iVideoSystemID"=>1000001,
"id"=>nil}
Then you'll have to choose 4 parameters to send. I just took last 4 parameters
[14] pry(main)> chosen_params = hash.slice("ItemID", "id", "iVideoSystemID", "ADDRESS_Zip")
=> {"ItemID"=>12345, "id"=>nil, "iVideoSystemID"=>1000001, "ADDRESS_Zip"=>"90210"}
Then you'll have to pass them to your remote url. This can be done using a helper described here. Then you'll have to just do something like generate_url("YOUR-URL-ADDR-HERE", chosen_params).
At this point you might want to change the generate_url helper in a way you need it to be to generate the url you need. Maybe it should take third parameter called action which will then generate url like http://www.google.com/action?{chosen_params}
The result will be:
[23] pry(main)> generate_url("http://www.google.com", chosen_params)
=> "http://www.google.com?ADDRESS_Zip=90210&ItemID=12345&iVideoSystemID=1000001&id="
Hope it helps. Let me know about any questions.
Could you modify the JSON response?
{
"AccountCode": "1234",
"AccountID": 13579,
...
"id": null
"follow_up_url": "http://myremoteURL.com/Service/?Param1=sSerialNumber&Param2=iVideoSystemID&Param3=sMAC&Param4=ItemID"
}
This allows your JSON to tell the requester "where to go next".

Mandrill ignores line breaks (\n)

Using the send-template method and the Lutung Mandrill Java API implementation, when a template variable contains line breaks of the form \n, they are totally ignored or stripped on generated emails. The Strings containing \n characters are kept untouched untill the GET send-template method is executed. So I guess there is some String analysis and conversion on Mandrill servers which removes special characters.
How can I make work again line breaks on Mandrill emails?
In my case I am using Handlebars for mandrill templates and I have solved this by sending HTML to the template <br/> instead of \n by replacing \n in my string with <br/> something like: .Description.Replace("\n", "<br/>");
And then on the mandrill template I put the variable inside {{{ variable }}} instead of {{ variable }}
http://blog.mandrill.com/handlebars-for-templates-and-dynamic-content.html
HTML escaping
Handlebars HTML-escapes values returned by an
{{expression}}. If you don't want Handlebars to escape a value, use
the "triple-stash", "{{{.
In your template:
<div> {{{html_tag_content}}} </div> In your API request:
"global_merge_vars": [ {
"name": "html_tag_content",
"content": "This example<br>is all about<br>the magical world of handlebars" } ]
The result:
This example
is all about
the magical world of handlebars
If you want to send more complex content, be it html, or variables with break lines and whatnot, you can first render the template and then send the message, instead of directly using send-template.
Render the template with a call to templates.render:
{
"key": "example key",
"template_name": "Example Template",
"template_content": [
{
"name": "editable",
"content": "<div>content to inject *|MERGE1|*</div>"
}
],
"merge_vars": [
{
"name": "merge1",
"content": "merge1 content\n contains line breaks"
}
]
}
Save the result to a variable rendered_content.
Send the message using the rendered template with messages.send:
{
"message": {
"html": rendered_content,
...
...
}

Unable to create course via api

I am trying to create a course in a semester through the api in valence d2l. I keep getting a 404 not found error, both in my program and in the "getting started" application. The call I am making is to /d2l/api/lp/1.0/courses/ using post. I pass the following JSON object along with it:
{
"Name": "COMM291 - Test A",
"Code": "C-COMM291",
"Path": "/enforced/C-COMM291/",
"CourseTemplateId": 20992,
"SemesterId": 20993,
"StartDate": "2013-08-22T19:41:14.0983532Z",
"EndDate": "2013-08-27T19:41:14.0993532Z",
"LocaleId": 4105,
"ForceLocale": false,
"ShowAddressBook": false
}
I have also tried passing null for the fields that say they accept null values, but no luck. The course template and the semester ID are correct - I have tripled checked that they exist, I am enrolled in them and I am using the correct ID numbers.
Try reducing the precision in your start and end dates to three decimals after the final point (e.g., "2013-08-22T19:41:14.0983532Z" becomes "2013-08-22T19:41:14.098Z").
If your org is configured to automatically enforce, and generate, paths for course offerings, then you should not provide one in your CreateCourseOffering block at all. The following structure works on our test instance: notice the empty string for path (shouldn't be null, but an empty string, I believe):
{ "Name": "Extensibility 104",
"Code": "EXT-104",
"Path": "",
"CourseTemplateId": 8082,
"SemesterId": 6984,
"StartDate": "2013-09-01T19:41:14.098Z",
"EndDate": "2013-12-27T19:41:14.098Z",
"LocaleId": 1,
"ForceLocale": false,
"ShowAddressBook": false }
The other thing to note is that if your CreateCourse form doesn't have a form element to provide a Semester ID, then your API call should pass null for that property.
I found that part of my problem was with the call if I change it to /d2l/api/lp/1.3/courses/ instead of 1.0 it works, (1.0 will work but it seems that you can only pass null for the semester).
The dates were also picky and did prefer milliseconds to only 3 decimal places.
Then passing null for LocaleId also helped.

Resources