locked
How do I post JSON String in console application RRS feed

  • Question

  • User-1971168174 posted

    Hi Support,

    I can use Postman or Insomnia to post the data and get result, same way i want to implement in console application,but i am unable to find way to pass my json string 

    Below is the json string i am passing to post using postman or insomnia

    	{
    	"source": {
    		"id": "cdl",
    		"emp": "test",
    		"pass": "0000",
    		"work": {
    			"typ": "lo",
    			"stn": "mo"
    		}
    	},
    	"ver": "2.0",
    	"tok": "",
    	"alive": false,
    	"single": true,
    	"trans": [
    		{
    			"cms": [
    				{
    					"cm": {
    						"name": "findname",
    						"u_no": "test",
    						"u_part_n": ""
    					}
    				}
    			],
    			"u": {
    				"u_id": "test",
    				"p_no": "",
    				"qty": -1,
    				"rev": ""
    			},
    			"r_unit": true
    		}
    	]
    }
    
    

    Now I am using below code to pass it inside console application but looks it is not the correct way

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using System.Net;
    using System.Net.NetworkInformation;
    using System.Net.Sockets;
    using System.Net.Http;
    
    using System.Data.SqlClient;
    using System.Data;
    using System.IO;
    
    
    namespace PostAPI
    {
        class Program
        {
            static void Main(string[] args)
            {
                MainAsync().Wait();
            }
            static async Task MainAsync()
            {
                string myJson = @"	{
    	'source': {
    		'id': 'cdl',
    		'emp': 'test',
    		'pass': '0000',
    		'work': {
    			'typ': 'lo',
    			'stn': 'mo'
    		}
    	},
    	'ver': '2.0',
    	'tok': '',
    	'alive': false,
    	'single': true,
    	'trans': [
    		{
    			'cms': [
    				{
    					'cm': {
    						'name': 'findname',
    						'u_no': 'test',
    						'u_part_n': ''
    					}
    				}
    			],
    			'u': {
    				'u_id': 'test',
    				'p_no': '',
    				'qty': -1,
    				'rev': ''
    			},
    			'r_unit': true
    		}
    	]
    }
    
    
    ";
    
                using (var client = new HttpClient())
                {
                    var response = await client.PostAsync("https://xyz.w.com:99006/test", new StringContent(myJson, Encoding.UTF8, "application/json"));
                }
    
            }
        }
    }
    

    Error:{StatusCode: 500, ReasonPhrase: '[json.exception.parse_error.101] parse error at line 2, column 2: syntax error while parsing object key - invalid literal; last read: '<U+0009>{<U+000D><U+000A><U+0009>â'; expected string literal'

    Thanks!

    Tuesday, May 19, 2020 11:25 AM

Answers

  • User-474980206 posted

    Your json is not valid. Unlike JavaScript, single quotes are not supported.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 19, 2020 2:43 PM
  • User-1971168174 posted

    It accepted double quotes like below

    ""source"": {
    		""id"": ""cdl"",
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2020 5:02 AM

All replies

  • User-474980206 posted

    Your json is not valid. Unlike JavaScript, single quotes are not supported.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 19, 2020 2:43 PM
  • User1535942433 posted

    Hi neerajkumarmodi,

    Accroding to your description,as far as I think,your json string format is wrong.

    Just like this:

    var json = "{ \"source\": {\"id\":\"cdl\",\"emp\":\"test\",\"0000\"}," +
    "\"ver\":\"2.0\"};
    var record = JsonConvert.DeserializeObject(json);

    Best regards,

    Yijing Sun

    Wednesday, May 20, 2020 7:21 AM
  • User753101303 posted

    Hi,

    Another option could be to take advantage of the PostAsJsonAsync extension method: https://docs.microsoft.com/en-us/previous-versions/aspnet/hh944339(v=vs.118)

    This way you can post a .NET object (possibly declared using an anonymous type) and let a json serializer to produce the corresponding json for you.

    Wednesday, May 20, 2020 7:49 AM
  • User-1971168174 posted

    It accepted double quotes like below

    ""source"": {
    		""id"": ""cdl"",
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2020 5:02 AM
  • User1373598504 posted

    Hi,

     I would suggest you first create the object then serialization in JSON format.

    Saturday, May 23, 2020 5:46 PM