0

I have a super annoying issue trying to post some JSON to the Service Now API (Service Desk software.

It works perfectly when sending from PowerShell, but in C# I just get

"StatusCode: BadRequest, Content-Type: application/json; charset=UTF-8, Content-Length: -1)"

With message

"{\"error\":{\"detail\":\"Cannod decode: java.io.StringReader@399624\",\"message\":\"Exception while reading request\"},\"status\":\"failure\"}"

The request JSON is exactly the same, apart from the fact that C# needs the "", {{ and }} escape characters.

When debugging, I pasted the saved body string and validated it against a json validator.

How am I supposed to debug this? Really appreciate any help at all.

C#

var client = new RestClient("https://instance.service-now.com");

var request = new RestRequest("api/now/v1/table/incident", Method.POST);
client.Authenticator = new HttpBasicAuthenticator("bassie.surname", pw());
request.RequestFormat = DataFormat.Json;
request.AddJsonBody(body);
request.UseDefaultCredentials = true;

var response = client.Execute(request);

where

    var body = $@"
{{
    ""parent"":  """",
    ""u_attachment_records"":  """",
    ""u_bsc_service_type"":  """",
    ""caused_by"":  """",
    ""watch_list"":  """",
    ""upon_reject"":  ""cancel"",
    ""sys_updated_on"":  ""{now}"",
    ""approval_history"":  """",
    ""u_is_parent"":  ""false"",
    ""skills"":  """",
    ""u_classification"":  ""{ticket.Classification}"",
    ""u_affected_contact"":  ""{ticket.AffectedContact}"",
    ""state"":  ""{inProgressState}"",
    ""u_last_time_stamp"":  """",
    ""u_major_incident_manager"":  """",
    ""u_next_step"":  """",
    ""sys_created_by"":  ""{ticket.AssignedTo}"",
    ""u_category_grouping"":  ""IT"",
    ""knowledge"":  ""false"",
    ""order"":  """",
    ""u_attachment_logs"":  """",
    ""u_attachments"":  """",
    ""cmdb_ci"":  """",
    ""delivery_plan"":  """",
    ""u_previous_assignment_groups"":  """",
    ""impact"":  ""{lowUrgency}"",
    ""active"":  ""true"",
    ""priority"":  ""{lowPriority}"",
    ""u_previous_number"":  """",
    ""group_list"":  """",
    ""u_bt_correlation_id"":  """",
    ""approval_set"":  """",
    ""u_bsc_accounting_period"":  """",
    ""u_r3"":  ""false"",
    ""short_description"":  ""{ticket.Description}"",
    ""correlation_display"":  """",
    ""delivery_task"":  """",
    ""work_start"":  """",
    ""u_third_party"":  """",
    ""u_requestor_rejection_count"":  ""0"",
    ""additional_assignee_list"":  """",
    ""u_requestor"":  ""{ticket.AffectedContact}"",
    ""notify"":  ""1"",
    ""sys_class_name"":  ""{ticket.Classification}"",
    ""u_sla_breach_code"":  """",
    ""closed_by"":  """",
    ""follow_up"":  """",
    ""u_contact_details"":  """",
    ""reassignment_count"":  ""0"",
    ""assigned_to"":  ""{ticket.AssignedTo}"",
    ""sla_due"":  """",
    ""u_workstation_location"":  """",
    ""u_resolver_watch_list"":  """",
    ""u_created_from"":  """",
    ""u_my_work_priority"":  """",
    ""escalation"":  ""0"",
    ""upon_approval"":  ""proceed"",
    ""u_effort"":  """",
    ""u_incident_model"":  """",
    ""correlation_id"":  """",
    ""u_approver_list"":  """",
    ""u_previous_step"":  """",
    ""u_gbs_id"":  """",
    ""u_inc_sub_status"":  """",
    ""u_customer_acceptance"":  """",
    ""made_sla"":  ""true"",
    ""u_stakeholder_group_list"":  """",
    ""u_major_incident"":  ""false"",
    ""u_bsc_service_group"":  """",
    ""u_request_id"":  """",
    ""u_building"":  """",
    ""u_stakeholder_watch_list"":  """",
    ""sys_updated_by"":  ""system"",
    ""opened_by"":  ""{ticket.CreatedBy}"",
    ""u_bsc_legal_entity"":  """",
    ""u_bsc_operating_unit"":  """",
    ""user_input"":  """",
    ""sys_created_on"":  ""{now}"",
    ""sys_domain"":  {{
                       ""link"":  ""https://instance.service-now.com/api/now/table/sys_user_group/global"",
                       ""value"":  ""global""
                   }},
    ""u_adms_assignedto_count"":  ""0"",
    ""u_escalate_ticket"":  ""false"",
    ""u_bsc_quality_check_result"":  """",
    ""calendar_stc"":  ""505043"",
    ""u_total_effort"":  """",
    ""u_escalate_ticket_date"":  """",
    ""u_resolution_reason"":  """",
    ""u_internal_ref"":  """",
    ""u_enhancement_defect_id"":  """",
    ""business_service"":  """",
    ""u_adms_assigned"":  """",
    ""u_hcl_correlation_id"":  """",
    ""u_symptom"":  ""general_failure"",
    ""rfc"":  """",
    ""time_worked"":  """",
    ""u_quick_closed"":  ""false"",
    ""expected_start"":  """",
    ""opened_at"":  ""{now}"",
    ""u_ebond_lock"":  ""false"",
    ""work_end"":  """",
    ""caller_id"":  """",
    ""u_dsyg_project_name"":  """",
    ""u_resolver_group_list"":  """",
    ""subcategory"":  """",
    ""work_notes"":  """",
    ""assignment_group"":  {{
                             ""link"":  ""https://instance.service-now.com/api/now/table/sys_user_group/a5c69cd2c55f01c427c9c96cacebf391"",
                             ""value"":  ""a5c69cd2c55f01c427c9c96cacebf391""
                         }},
    ""business_stc"":  ""126137"",
    ""u_dsyg_project_number"":  """",
    ""calendar_duration"":  ""1970-01-06 20:17:23"",
    ""u_external_ref"":  """",
    ""close_notes"":  """",
    ""contact_type"":  ""phone"",
    ""u_acceptance_method"":  ""Automatic"",
    ""u_third_party_reference"":  """",
    ""incident_state"":  ""1"",
    ""urgency"":  ""{lowUrgency}"",
    ""problem_id"":  """",
    ""u_adms_first_assignto"":  ""false"",
    ""company"":  """",
    ""activity_due"":  """",
    ""severity"":  ""3"",
    ""u_last_attached_kb"":  """",
    ""comments"":  """",
    ""approval"":  ""not requested"",
    ""due_date"":  """",
    ""sys_mod_count"":  ""1"",
    ""sys_tags"":  """",
    ""u_resolution_code"":  ""resolved_unknown"",
    ""u_resolution_activities"":  """",
    ""u_service_zone"":  {{
                           ""link"":  ""https://instance.service-now.com/api/now/table/u_service_zone/8f2b4c9a382de00097ba6b481956396b"",
                           ""value"":  ""8f2b4c9a382de00097ba6b481956396b""
                       }},
    ""u_stakeholder_communications"":  """",
    ""location"":  {{
                     ""link"":  ""https://instance.service-now.com/api/now/table/cmn_location/eafd7038517de000c86dc462c016ea6b"",
                     ""value"":  ""eafd7038517de000c86dc462c016ea6b""
                 }},
    ""category"":  """",
    ""u_bsc_service_category"":  """"
}}
";
Bassie
  • 9,529
  • 8
  • 68
  • 159
  • You should be using Newtonsoft to build the json payload. This is way too fragile and error prone –  Mar 12 '18 at 04:50
  • I'm sure that if you inspect the request using Fiddler, you'll see that it is *not* valid. –  Mar 12 '18 at 04:51
  • @Amy Thanks for your comments. I don't really get what you mean - I just want to send a simple json string to test that the endpoint works - How would I go about building the payload with newtonsoft? – Bassie Mar 12 '18 at 04:53
  • https://www.newtonsoft.com/json/help/html/SerializingJSON.htm –  Mar 12 '18 at 04:58
  • @Amy Thanks amy I'll have a look. What I mean though, is why is it so difficult to just send it as a string? Surely I can validate it on my side before sending it, and that will ensure it is well-formed? Or am I missing something – Bassie Mar 12 '18 at 05:00
  • Lol, there is nothing simple about your json, and using string formatting makes things incredibly difficult, not easier. Again, inspect your request in Fiddler and you'll see that it is not well formed. –  Mar 12 '18 at 05:01
  • @Amy What I mean is, isn't all JSON pretty simple in that it is quite a straghtforward format? I don't exactly have very complicated JSON here so I don't really get what you're saying.. How can it be so difficult to just send a plain string? Anyways I got it working. – Bassie Mar 12 '18 at 05:10
  • It's not good idea to build json string like this, because (among other things) - you don't escape values you use there. For example, if `ticket.Classification` (or any other field) contains quote character - your whole json will be invalid. – Evk Mar 12 '18 at 05:53
  • Possible duplicate of [RestSharp JSON Parameter Posting](https://stackoverflow.com/questions/6312970/restsharp-json-parameter-posting) –  Mar 12 '18 at 06:57

0 Answers0