Jump to content

Recommended Posts

Posted (edited)

Hi friends,

I'm using json.au3 to register some data in my server, everything work fine except the data that i send in utf8 format like names saved as ????? .

i think something mess with inetget cause this problem

can you help me please?

$sAdditionalData = "secret_key=" & $secret_key_verify & "&slm_action=slm_check&license_key=" & $license_key&"&first_name="&$first_name
$data = _INetGetSource($sDomain&'/?'&$sAdditionalData)
$object = json_decode($data)
Json_Dump($data)
$response = json_get($object, '.first_name')

 

Edited by rot3r
Posted

its $sAdditionalData (the first_name output is correctly shown )

1239965344_Screenshot2022-02-22150350.png.8483146ebf29f8b0dc23f805dda71c66.png

 

its $data output (the first name field changed to ???????)
767294689_Screenshot2022-02-22150411.png.898c7a1813722d0e2de912b5d101c925.png

  • Solution
Posted (edited)

Most browsers will try to properly encode a URL before sending it.  As you have found out, the _InetGetSource() function does not do any special encoding of your URL.  Therefore, you must make sure that your URL is properly encoded before sending it.  The example below shows one of many functions (_UnicodeURLEncode) that can be used to encode all or part of a URL.  You can find another example in the topic below.  If you are building URLs dynamically, it's probably a good idea to encode the URL whether you think you need to or not.

 

 

Example:

#AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d

#include <Constants.au3>
#include <Inet.au3>
#include <Debug.au3>
#include <json.au3>


example()

Func example()
    Local $sEncodedRequest, $sRespone, $sJson
    Local $oJson

    Const $REQUEST = "https://postman-echo.com/get?first_name=سلام"

    _DebugSetup(Default, False, 5)

    ;Encode & display the URL
    $sEncodedRequest = _UnicodeURLEncode("https://postman-echo.com/get?first_name=سلام")

    _DebugOut("HTTP Request (Raw)     = " & $REQUEST)
    _DebugOut("HTTP Request (Encoded) = " & $sEncodedRequest)

    ;Send HTTP GET request
    $sRespone = _INetGetSource($sEncodedRequest, False)
    If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "_INetGetSource failed! @error = " & @error)

    ;Decode JSON response
    $oJson = Json_Decode(BinaryToString($sRespone, $SB_UTF8))
    If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", "Unable to decode response. @error = " & @error)

    ;Display pretty-printed response (unicode and slashes are unescaped)
    $sJson = Json_Encode($oJson, $JSON_PRETTY_PRINT + $JSON_UNESCAPED_UNICODE + $JSON_UNESCAPED_SLASHES, "   ")

    _DebugOut(@CRLF & "JSON Response (unicode and slashes are unescaped)")
    _DebugOut($sJson)
EndFunc

;===============================================================================
; _UnicodeURLEncode()
; Description: : Encodes an unicode string to be URL-friendly
; Parameter(s): : $UnicodeURL - The Unicode String to Encode
; Return Value(s): : The URL encoded string
; Author(s): : Dhilip89
; Note(s):
; https://www.autoitscript.com/forum/topic/46894-unicodeurl-udf/
;===============================================================================
Func _UnicodeURLEncode($UnicodeURL)
    Local $UnicodeBinary = StringToBinary ($UnicodeURL, 4)
    Local $UnicodeBinary2 = StringReplace($UnicodeBinary, '0x', '', 1)
    Local $UnicodeBinaryLength = StringLen($UnicodeBinary2)
    Local $UnicodeBinaryChar
    Local $EncodedString
    For $i = 1 To $UnicodeBinaryLength Step 2
        $UnicodeBinaryChar = StringMid($UnicodeBinary2, $i, 2)
        If StringInStr("$-_.+!*'(),;/?:@=&abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", BinaryToString ('0x' & $UnicodeBinaryChar, 4)) Then
            $EncodedString &= BinaryToString ('0x' & $UnicodeBinaryChar)
        Else
            $EncodedString &= '%' & $UnicodeBinaryChar
        EndIf
    Next
    Return $EncodedString
EndFunc

Example output

HTTP Request (Raw)     = https://postman-echo.com/get?first_name=سلام
HTTP Request (Encoded) = https://postman-echo.com/get?first_name=%D8%B3%D9%84%D8%A7%D9%85

JSON Response (unicode and slashes are unescaped)
{
   "args": {
      "first_name": "سلام"
   },
   "headers": {
      "x-forwarded-proto": "https",
      "x-forwarded-port": "443",
      "host": "postman-echo.com",
      "x-amzn-trace-id": "Root=1-6215610f-575e35fb5529a4b26d20bf55",
      "user-agent": "AutoIt",
      "cache-control": "no-cache"
   },
   "url": "https://postman-echo.com/get?first_name=%D8%B3%D9%84%D8%A7%D9%85"
}

 

Edited by TheXman

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
×
×
  • Create New...