Jump to content

in post method receive arabic character incorrect


Go to solution Solved by jchd,

Recommended Posts

in autoit i send a json with post method to url with custom header like:

$sString1 = "https://license.status-st.com/api/check"
$sString2 = '{"' & "license" & '":"' & "E9K57WAZJBUMNDK" & '","' & "machine_fingerprint" & '":"' & "12311434" & '","' & "product_name" & '":"' & "sepp" & '"}'
$HTTP_PST = _HTTP_Post__($sString1, $sString2)

Custom header:

$oHTTP.SetRequestHeader("Api-key", "yc7RxTVq8sZNBw2fvWjM634zNGsEFW4nvtDSdnQm")
$oHTTP.SetRequestHeader("Accept", "application/json")
$oHTTP.SetRequestHeader("Content-Type", "application/json")

in returned value  have arabic  characters, that arabic characters display incorrected like:

{"message":"Invalid license","errors":{"key":"\u0627\u06cc\u0646 \u0644\u0627\u06cc\u0633\u0646\u0633 \u062f\u0631 \u0633\u06cc\u0633\u062a\u0645 \u0645\u0648\u062c\u0648\u062f \u0646\u06cc\u0633\u062a"}}

 

but postman return value like this:

{
    "message""Invalid license",
    "errors": {
        "key""این لایسنس در سیستم موجود نیست"
    }
}
 
What is your solution to the problem?
thanks
Edited by r2du-soft
Link to post
Share on other sites
  • r2du-soft changed the title to in post method receive arabic character incorrect
  • Solution
Posted (edited)

Try this:

Local $s = '{"message":"Invalid license","errors":{"key":"\u0627\u06cc\u0646 \u0644\u0627\u06cc\u0633\u0646\u0633 \u062f\u0631 \u0633\u06cc\u0633\u062a\u0645 \u0645\u0648\u062c\u0648\u062f \u0646\u06cc\u0633\u062a"}}'
Local $t = Execute("'" & StringRegExpReplace($s, "\\u([[:xdigit:]]{4})", "' & ChrW(0x$1) & '") & "'")
MsgBox(0, "", $t)

This is standard JSON encoding for general Unicode.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites

The reverse operation is no harder:

; JSON Unicode to AutoIt UCS2 string
Local $s = '{"message":"Invalid license","errors":{"key":"\u0627\u06cc\u0646 \u0644\u0627\u06cc\u0633\u0646\u0633 \u062f\u0631 \u0633\u06cc\u0633\u062a\u0645 \u0645\u0648\u062c\u0648\u062f \u0646\u06cc\u0633\u062a"}}'
Local $t = Execute("'" & StringRegExpReplace($s, "\\u([[:xdigit:]]{4})", "' & ChrW(0x$1) & '") & "'")
MsgBox(0, "", $t)

; AutoIt UCS2 string to JSON Unicode
Local $u = Execute("'" & StringRegExpReplace($t, "([\x{0100}-\x{FFFF}])", "\\u' & StringLower(Hex(AscW('$1'), 4)) & '") & "'")
MsgBox(0, "", $u)

ConsoleWrite('JSON strings are ' & ($s == $u ? 'equal' : 'different!') & @LF)

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites

There are various standards for such encoding. Here's a good sample based on https://r12a.github.io/app-conversion/index.html

Local $s = 'این لایسنس در سیستم موجود نیست'    ; AutoIt UCS2 string with characters > 0xFF (or not)
Local $t    ; conversion to encoded form
Local $u    ; $t converted back to AutoIt UCS2 string

; AutoIt UCS2 string to JSON/JS/Java/C C-style Unicode
Local $t = Execute("'" & StringRegExpReplace($s, "([\x{0100}-\x{FFFF}])", "\\u' & Hex(AscW('$1'), 4) & '") & "'")
MsgBox(0, "", $t)
cw($t)

; JSON/JS/Java/C C-style Unicode to AutoIt UCS2 string
Local $u = Execute("'" & StringRegExpReplace($t, "\\u([[:xdigit:]]{1,4})", "' & ChrW(0x$1) & '") & "'")
MsgBox(0, "", $u)
cw($u)

cw('Round-trip conversion to JSON/JS/Java/C C-style string is ' & ($s == $u ? 'correct' : 'incorrect!'))
cw()

; AutoIt UCS2 string to JSON/JS/Java/C ES6 or Rust/Ruby Unicode
Local $t = Execute("'" & StringRegExpReplace($s, "([\x{0100}-\x{FFFF}])", "\\u\{' & Hex(AscW('$1'), 4) & '}") & "'")
MsgBox(0, "", $t)
cw($t)

; JSON/JS/Java/C ES6 or Rust/Ruby Unicode to AutoIt UCS2 string
Local $u = Execute("'" & StringRegExpReplace($t, "\\u\{([[:xdigit:]]{1,4})\}", "' & ChrW(0x$1) & '") & "'")
MsgBox(0, "", $u)
cw($u)

cw('Round-trip conversion to JSON/JS/Java/C ES6 or Rust/Ruby string is ' & ($s == $u ? 'correct' : 'incorrect!'))
cw()

; AutoIt UCS2 to Hex NCRs
Local $t = Execute("'" & StringRegExpReplace($s, "([\x{0100}-\x{FFFF}])", "&#x' & Hex(AscW('$1'), 4) & '") & "'")
MsgBox(0, "", $t)
cw($t)

; Hex NCRs to AutoIt UCS2
Local $u = Execute("'" & StringRegExpReplace($t, "&#x([[:xdigit:]]{1,4})", "' & ChrW(0x$1) & '") & "'")
MsgBox(0, "", $u)
cw($u)

cw('Round-trip conversion to Hex NCRs string is ' & ($s == $u ? 'correct' : 'incorrect!'))
cw()

; AutoIt UCS2 to Decimal NCRs
Local $t = Execute("'" & StringRegExpReplace($s, "([\x{0100}-\x{FFFF}])", "&#x' & AscW('$1') & '") & "'")
MsgBox(0, "", $t)
cw($t)

; Decimal NCRs to AutoIt UCS2
Local $u = Execute("'" & StringRegExpReplace($t, "&#x(\d{1,5})", "' & ChrW($1) & '") & "'")
MsgBox(0, "", $u)
cw($u)

cw('Round-trip conversion to Decimal NCRs string is ' & ($s == $u ? 'correct' : 'incorrect!'))
cw()

; AutoIt UCS2 string to CSS Unicode
Local $t = Execute("'" & StringRegExpReplace($s, "([\x{0100}-\x{FFFF}])", "\\' & Hex(AscW('$1'), 4) & ' ") & "'")
MsgBox(0, "", $t)
cw($t)

; CSS Unicode to AutoIt UCS2 string
Local $u = Execute("'" & StringRegExpReplace($t, "\\([[:xdigit:]]{1,4}) ", "' & ChrW(0x$1) & '") & "'")
MsgBox(0, "", $u)
cw($u)

cw('Round-trip conversion to CSS string is ' & ($s == $u ? 'correct' : 'incorrect!'))
cw()

; AutoIt UCS2 string to Perl/UTR#18 Unicode
Local $t = Execute("'" & StringRegExpReplace($s, "([\x{0100}-\x{FFFF}])", "\\x\{' & Hex(AscW('$1'), 4) & '\}") & "'")
MsgBox(0, "", $t)
cw($t)

; Perl/UTR#18 Unicode to AutoIt UCS2 string
Local $u = Execute("'" & StringRegExpReplace($t, "\\x\{([[:xdigit:]]{1,4})\}", "' & ChrW(0x$1) & '") & "'")
MsgBox(0, "", $u)
cw($u)

cw('Round-trip conversion to Perl/UTR#18 string is ' & ($s == $u ? 'correct' : 'incorrect!'))
cw()

; Unicode-aware ConsoleWrite
Func cw($v)
    ConsoleWrite(BinaryToString(StringToBinary($v & @LF, 4), 1))
EndFunc

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites

Since you are working with JSON, you could also use UDF's designed specifically for that purpose, like the ones in JSON.au3, which can handle JSON escaping & unescaping for you:

#include <Constants.au3>
#include <Debug.au3>
#include <MyIncludes\json\json.au3> ;<== Modify path as needed


example()

Func example()
    Const $JSON = '{"message":"Invalid license","errors":{"key":"\u0627\u06cc\u0646 '    & _
                  '\u0644\u0627\u06cc\u0633\u0646\u0633 \u062f\u0631 \u0633\u06cc\u0633' & _
                  '\u062a\u0645 \u0645\u0648\u062c\u0648\u062f \u0646\u06cc\u0633\u062a"}}'

    Local $oJson = Null


    ;Set up debug messages to go to notepad
    _DebugSetup("JSON Parsing Example", False, 5)

    ;Decode JSON into dictionary object
    $oJson = Json_Decode($JSON)
    If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "Json_Decode failed - @error = " & @error)

    ;Display examples of escaped and unescaped JSON unicode output
    _DebugOut("Raw JSON")
    _DebugOut(Json_Encode($oJson) & @CRLF)

    _DebugOut("Raw JSON with unescaped unicode")
    _DebugOut(Json_Encode($oJson, $JSON_UNESCAPED_UNICODE) & @CRLF)

    _DebugOut("Pretty-printed JSON")
    _DebugOut(Json_Encode($oJson, $JSON_PRETTY_PRINT) & @CRLF)

    _DebugOut("Pretty-printed JSON with unescaped unicode")
    _DebugOut(Json_Encode($oJson, $JSON_PRETTY_PRINT + $JSON_UNESCAPED_UNICODE) & @CRLF)

    _DebugOut("Parsed JSON values")
    _DebugOut(".message    = " & Json_Get($oJson, ".message"))
    _DebugOut(".errors     = " & Json_Encode(Json_Get($oJson, ".errors"), $JSON_UNESCAPED_UNICODE))
    _DebugOut(".errors.key = " & Json_Get($oJson, ".errors.key"))
EndFunc

Output:

Raw JSON
{"message":"Invalid license","errors":{"key":"\u0627\u06cc\u0646 \u0644\u0627\u06cc\u0633\u0646\u0633 \u062f\u0631 \u0633\u06cc\u0633\u062a\u0645 \u0645\u0648\u062c\u0648\u062f \u0646\u06cc\u0633\u062a"}}

Raw JSON with unescaped unicode
{"message":"Invalid license","errors":{"key":"این لایسنس در سیستم موجود نیست"}}

Pretty-printed JSON
{
   "message": "Invalid license",
   "errors": {
      "key": "\u0627\u06cc\u0646 \u0644\u0627\u06cc\u0633\u0646\u0633 \u062f\u0631 \u0633\u06cc\u0633\u062a\u0645 \u0645\u0648\u062c\u0648\u062f \u0646\u06cc\u0633\u062a"
   }
}

Pretty-printed JSON with unescaped unicode
{
   "message": "Invalid license",
   "errors": {
      "key": "این لایسنس در سیستم موجود نیست"
   }
}

Parsed JSON values
.message    = Invalid license
.errors     = {"key":"این لایسنس در سیستم موجود نیست"}
.errors.key = این لایسنس در سیستم موجود نیست

 

Edited by TheXman
Added more output examples
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...