Skip to main content
Participant
November 26, 2020
Answered

.Net core api 2.2 request to Coldfusion Api character issue inserting to MSSQL db

  • November 26, 2020
  • 2 replies
  • 317 views

I make the request to coldfusion api from .net core api as shown below

 

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

                var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method = "POST";

                var jsonSeri_ = Newtonsoft.Json.JsonConvert.SerializeObject(_frmCustomerTicket);

                Encoding iso = Encoding.GetEncoding("iso-8859-9");
                Encoding utf8 = Encoding.UTF8;
                byte[] utfBytes = utf8.GetBytes(jsonSeri_);
                byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
                string msg = iso.GetString(isoBytes);

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {

                    streamWriter.Write(msg);

                }

 

in coldfusion api I get the request json as below

<cfif cgi.content_type EQ "application/json">
      <cfset _deserializeJSON = deserializeJSON(ToString(getHTTPRequestData().content))>
    <cfelse>
{"userFname":"","userEmail":"","companyId":1,"priority_id":3,"userLTopic":"donanım arızası","pdesc":"cookie güzel bir şekilde çalışıyorrrrr"}

 

then, I send the json data to another page to insert the data to MSSQL database.

this is how I pass the data:

 <cfset var formStruct = _deserializeJSON />
 <cfinclude template="queryexecute.cfm">

In another page, I use the code as below.

 

<cfprocessingdirective pageEncoding="utf-8">

<cfparam name="formStruct" default="">

INSERT INTO
  G_SERVICE
   (
                            
     SERVICE_ACTIVE,
     ISREAD,
     SERVICECAT_ID,
     SERVICE_STATUS_ID,
     PRIORITY_ID,
     COMMETHOD_ID,
     SERVICE_HEAD,
     SERVICE_DETAI
 )
 VALUES
  ( 
    1
    , 0
    , 1
    , 92
    , #formStruct.priority_id#
    , 0
    , <cfqueryparam cfsqltype="CF_SQL_NVARCHAR" value="#(formStruct.userLTopic)#">
    , <cfqueryparam cfsqltype="CF_SQL_NVARCHAR" value="<p>  #(formStruct.pdesc)#  </p>">
    , <cfqueryparam cfsqltype="CF_SQL_TIMESTAMP" value="#now()#">
    , <cfqueryparam cfsqltype="CF_SQL_TIMESTAMP" value="#now()#">
)

 The weird thing is that, when it is being inserted, the formstruct parameters changes as below:

 I can not get why it changes while being inserted as Ä±,ü .

{"userFname":"","userEmail":"","companyId":1,"priority_id":3,"userLTopic":"donanım arızası","pdesc":"cookie güzel bir şekilde çalışıyorrrrr"}

 Anyone has any idea why on earth would this happen?

    This topic has been closed for replies.
    Correct answer cagatayc60817597

    the only thing I have just added is charset=utf-8 to contentType. Let me share the whole code .net core web api 2.0.

    It is smoothly working now. 

    request.ContentType = "application/json; charset=utf-8";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://url");
                
      request.ContentType = "application/json; charset=utf-8";
      request.Method = "POST";
    
       using (var streamWriter = new StreamWriter(request.GetRequestStream()))
       {
           string json2 = JsonConvert.SerializeObject(_frmCustomerTicket);
           streamWriter.Write(json2);
        }
    
       HttpWebResponse response = (HttpWebResponse)request.GetResponse();
       var result = "";
       using (StreamReader reader = new StreamReader(response.GetResponseStream()))
         {
                    result = reader.ReadToEnd();
         }

     

    2 replies

    cagatayc60817597AuthorCorrect answer
    Participant
    November 27, 2020

    the only thing I have just added is charset=utf-8 to contentType. Let me share the whole code .net core web api 2.0.

    It is smoothly working now. 

    request.ContentType = "application/json; charset=utf-8";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://url");
                
      request.ContentType = "application/json; charset=utf-8";
      request.Method = "POST";
    
       using (var streamWriter = new StreamWriter(request.GetRequestStream()))
       {
           string json2 = JsonConvert.SerializeObject(_frmCustomerTicket);
           streamWriter.Write(json2);
        }
    
       HttpWebResponse response = (HttpWebResponse)request.GetResponse();
       var result = "";
       using (StreamReader reader = new StreamReader(response.GetResponseStream()))
         {
                    result = reader.ReadToEnd();
         }

     

    BKBK
    Community Expert
    Community Expert
    November 29, 2020

    Precisely. I was about to suggest that you add the UTF-8 charset to every call requiring encoding and to every CFM page involved.

    Participant
    November 27, 2020

    I sent the request via .net razor pages ajax and it did work, the context character set did not change. However when I intend to send the data via .net core web api 2.0, the context data breaks while being inserted to database. That is really strange. And I have to send the data via .net core web api