im using WebApi to make a webservice
as follow:
- using RestSharp;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- using Newtonsoft.Json;
- namespace test.Controllers
- {
- public class ValuesController : ApiController
- {
- public class WebAgent
- {
- public string Agent_no { get; set; }
- public string Agent_Name { get; set; }
- }
- public string GetAgent([FromBody] string a_body)
- {
- dynamic stuff = JsonConvert.DeserializeObject(a_body);
- string agent = stuff.agent_no.ToString();
- string name = stuff.agent_name.ToString();
- try
- {
- var test = new WebAgent()
- {
- Agent_no = agent,
- Agent_Name = name
- };
- JsonSerializerSettings settings = new JsonSerializerSettings
- {
- NullValueHandling = NullValueHandling.Ignore,
- Formatting = Formatting.Indented
- };
- var result= JsonConvert.SerializeObject(test,settings);
- return result;
- }
- catch (WebException e)
- {
- var message = e.Message;
- using (var reader = new StreamReader(e.Response.GetResponseStream()))
- {
- var content = reader.ReadToEnd();
- return content.ToString();
- }
- }
- finally
- {
- }
- }
- }
- }
but when i try to call my webapi service from postman i got the following response
- "{\r\n \"Agent_no\": \"122331\",\r\n \"Agent_Name\": \"sabatini\"\r\n}"
instead of:
i have tried to add- "{"Agent_no": "122331","Agent_Name": "sabatini"}"
- config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings
- {
- NullValueHandling = NullValueHandling.Ignore,
- Formatting = Formatting.Indented
- };
i need help !
thanks

Sachin SinghPosted Apr 18, 2021, 7:28 AM
Jignesh KumarPosted Apr 18, 2021, 7:21 AM