Attempting to use invalid JSON in Ruby? -


I am trying to test a test that returns a valid JSON 400 error code to a cURL request is. Unfortunately, Ruby will not allow me to use invalid JSON.

  if bad_json credentials = {"username" = & gt; Username "password" = & gt; Password} .to_json other credentials = {"username" = & gt; Username, "password" = & gt; Password} .to_json end   

When I try to run the test, it produces the following:

  /foo.rb:60: Syntax Error, unexpected tSTRING_BEG, expected '}' "password" = & gt; Password ^ /foo.rb:60: Syntax error, unexpected =>, Except keyword_end "password" = & gt; Is it possible to pass invalid JSON passwords without breaking the script?  

As others have commented that your Ruby has syntax errors, you must have a valid Ruby And if I understand your problem properly, then make errors in JSON.

Specifically, you are asking Ruby to make a hash, then convert it to a string in which the compliance treaty is made directly with JSON Speck, just make that text directly, then you control You can do it how closely it conforms. \ "# {Username} \" \ "password \": \ "# {password} \"} "Other credentials =" "if the bad_json credentials =" {\ "username" "{\" User Name \ ": \" # {username} \ ", \" password \ ": \" # {password} \ "}" end

edit - More details

To make this point more clear here is a simple example in irb / pry:

  [1] ) & gt; {} .class = & gt; Hash [2] PRI (Main) & gt; {} .to_json = & gt; "{}" [3] Pvt (main) & gt; {} .to_json.class = & gt; String [4] Pvt (main) & gt; "{}". Class = & gt; Here we can see that to_json gives only one string, so if you can create strings by yourself which is Confirmed JSON, then it will be as good as string JSON Make processor.   

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -