• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Nov 26, 2020 Nov 26, 2020

Copy link to clipboard

Copied

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?

Views

162

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Beginner , Nov 27, 2020 Nov 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.SerializeObj
...

Votes

Translate

Translate
Community Beginner ,
Nov 27, 2020 Nov 27, 2020

Copy link to clipboard

Copied

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 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 27, 2020 Nov 27, 2020

Copy link to clipboard

Copied

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();
     }

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 29, 2020 Nov 29, 2020

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation