Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

network.request filters the "+" character when sending data to a server
Started by solca Jun 28 2018 05:12 AM

6 replies to this topic
network request api restful php b64 image
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

solca

[GLOBAL: userInfoPane.html]
solca
  • Enthusiast

  • 53 posts
  • Corona SDK

Hi.

 

This is the situation:

 

Corona SDK Code (Client):

-- network.request filters the "+" character when sending data to a server
local myStringToSend = "This is my text=120+44+-2+abc-23" 

local function callback( event )
    if ( event.isError ) then
        print( "\nNetwork error: ", event.response )
    else
        print ( "\nRESPONSE: " .. event.response )
    end
 end
print ("\nmyStringToSend: "..myStringToSend)
local headers = {}  
headers["Content-Type"] = "application/x-www-form-urlencoded"
headers["Accept-Language"] = "en-US"
headers["User-Agent"] = "App Corona SDK"
local body = "MYSTRING="..myStringToSend
local params = {}
params.headers = headers
params.body = body
params.timeout = 12
url="https://xxxxxxxxxxxxx/returnString.php"
network.request( url, "POST", callback, params )

This is the Corona SDK Result:

Platform: Galaxy S8 (PHONEMAS) / x86_64 / 10.12.6 / Intel HD Graphics 4000 OpenGL Engine / 2.1 INTEL-10.25.19 / 2018.3313 / en-CO | CO | en_CO | en

myStringToSend: This is my text=120+44+-2+abc-23

RESPONSE: {"Status":"ok","myStringReturned":"This is my text=120 44 -2 abc-23"}

This is the PHP code (web service):

<?php
//
$myStringToReturn=$_POST["MYSTRING"];
//
{
   $results=array("Status"=>'ok',"myStringReturned"=>$myStringToReturn);
   http_response_code(200);
   header('Content-type: application/json');
   exit( json_encode($results) );
}


If I use another client to send the information, the results received are correct. (See attached image)

 

This could be a bug? Or I am doing something wrong?

 

Regards,

 

Solca

 

 

  



[TOPIC: post.html]
#2

solca

[GLOBAL: userInfoPane.html]
solca
  • Enthusiast

  • 53 posts
  • Corona SDK

Sorry: See attached image. Results from other client (No Corona sdk)

 

Attached Files



[TOPIC: post.html]
#3

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,995 posts
  • Corona SDK

You are running into what's known as URL encoding. I'm surprised you need it with HTTP POST, but when a string is safely transported across the Internet, it's "encoded" to take certain characters and protect them so the web server doesn't misinterpret them and then they get decoded on the other side. Symbols that are used as part of the URL and binary characters get encoded into a hex number, for instance, a space *could be* encoded into %20. Then the server would see the %20 and know to turn it back into a space.  However, space's have special rules. URL encoding can turn them into plus signs. When a string is URL decoded plus signs turn into spaces. 

 

So it sounds like the server is in someway decoding the POST data and converting them on you. You can use a URL encode function to protect the data before you send it. There are many different tools to do this. You could use URL encoding or Base64 encoding. Here is the URL encoding function I use:

function urlencode(str)
  if (str) then
	str = string.gsub (str, "\n", "\r\n")
	str = string.gsub (str, "([^%w ])",
		function (c) return string.format ("%%%02X", string.byte(c)) end)
	str = string.gsub (str, " ", "+")
  end
  return str    
end

Rob



[TOPIC: post.html]
#4

bbk

[GLOBAL: userInfoPane.html]
bbk
  • Enthusiast

  • 39 posts
  • Corona SDK

Some browsers interpret the "+" to be a space. I have replaced it with a special set of characters that I then change back to a "+" in the code on the server.



[TOPIC: post.html]
#5

solca

[GLOBAL: userInfoPane.html]
solca
  • Enthusiast

  • 53 posts
  • Corona SDK

Hi, thanks for replies.

 

If the failure were in the server, the response to other clients should be the same (bad), and it is not. With other clients the answer is correct (see attach image in previous post), then I do not think that the malfunction isin the server.
 
On the other hand, in this example (same Corona code and diff string): If my string is an image encoded in base 64, it happens the same: The "+" character does not reach the server.
 
 

myStringToSend: iVBORw0KGgoAAAANSUhEUgAAABwAAAASAQMAAAByySynAAAABlBMVEUAAAD///+l2Z/dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAP0lEQVQImWNgPm9gwAAmbM4bH4AQzAdAYiDC/rzxByTi/+f/cIL5AwPnZGYGIGHMwGA5mdkASNgbMNgJ80AIAMCSHqNvm2VtAAAAAElFTkSuQmCC

RESPONSE: {"Status":"ok","myStringReturned":"iVBORw0KGgoAAAANSUhEUgAAABwAAAASAQMAAAByySynAAAABlBMVEUAAAD\/\/\/ l2Z\/dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAP0lEQVQImWNgPm9gwAAmbM4bH4AQzAdAYiDC\/rzxByTi\/ f\/cIL5AwPnZGYGIGHMwGA5mdkASNgbMNgJ80AIAMCSHqNvm2VtAAAAAElFTkSuQmCC"}

See another image attach using other client restful

 

Regards,

 

solca

 

Attached Files

  • Attached File  bug2.png   47.46KB   0 downloads


[TOPIC: post.html]
#6

bbk

[GLOBAL: userInfoPane.html]
bbk
  • Enthusiast

  • 39 posts
  • Corona SDK

Here is a code that I am using. (Sorry, I cannot remember where I found it)

I use this in order to submit the value to the server in a URL request-string, but also to handle local characters

 local theDescription = alphaBitLib.convertTo1252URLEncode(theFields.theDescriptionField.text)

And here my code to do the encoding: 

-- HOW TO SOLVE THE national char problem...

-- http://stackoverflow.com/questions/9548275/network-request-and-non-english-characters

alphaBitLib.escape = function(s)

  s = string.gsub(s, "([&=+%c\128-\255])", function (c)
      return string.format("%%%02X", string.byte(c))
    end)
  s = string.gsub(s, " ", "+")
  return s
end
-- print(escape("http://server/aáäbcčdďeé..."))
-- > http://server/a%C3%A1%C3%A4bc%C4%8Dd%C4%8Fe%C3%A9...


local windows1252encodeArrayDouble = {
"%%20",
"%%80",
"%%81",
"%%82",
"%%83",
"%%84",
"%%85",
"%%86",
"%%87",
"%%88",
"%%89",
"%%8A",
"%%8B",
"%%8C",
"%%8D",
"%%8E",
"%%8F",
"%%90",
"%%91",
"%%92",
"%%93",
"%%94",
"%%95",
"%%96",
"%%97",
"%%98",
"%%99",
"%%9A",
"%%9B",
"%%9C",
"%%9D",
"%%9E",
"%%9F",
"%%A0",
"%%A1",
"%%A2",
"%%A3",
"%%A4",
"%%A5",
"%%A6",
"%%A7",
"%%A8",
"%%A9",
"%%AA",
"%%AB",
"%%AC",
"%%AD",
"%%AE",
"%%AF",
"%%B0",
"%%B1",
"%%B2",
"%%B3",
"%%B4",
"%%B5",
"%%B6",
"%%B7",
"%%B8",
"%%B9",
"%%BA",
"%%BB",
"%%BC",
"%%BD",
"%%BE",
"%%BF",
"%%C0",
"%%C1",
"%%C2",
"%%C3",
"%%C4",
"%%C5",
"%%C6",
"%%C7",
"%%C8",
"%%C9",
"%%CA",
"%%CB",
"%%CC",
"%%CD",
"%%CE",
"%%CF",
"%%D0",
"%%D1",
"%%D2",
"%%D3",
"%%D4",
"%%D5",
"%%D6",
"%%D7",
"%%D8",
"%%D9",
"%%DA",
"%%DB",
"%%DC",
"%%DD",
"%%DE",
"%%DF",
"%%E0",
"%%E1",
"%%E2",
"%%E3",
"%%E4",
"%%E5",
"%%E6",
"%%E7",
"%%E8",
"%%E9",
"%%EA",
"%%EB",
"%%EC",
"%%ED",
"%%EE",
"%%EF",
"%%F0",
"%%F1",
"%%F2",
"%%F3",
"%%F4",
"%%F5",
"%%F6",
"%%F7",
"%%F8",
"%%F9",
"%%FA",
"%%FB",
"%%FC",
"%%FD",
"%%FE",
"%%FF"
}
local utf8encodeArray = {
"%E2%82%AC",
"%81",
"%E2%80%9A",
"%C6%92",
"%E2%80%9E",
"%E2%80%A6",
"%E2%80%A0",
"%E2%80%A1",
"%CB%86",
"%E2%80%B0",
"%C5%A0",
"%E2%80%B9",
"%C5%92",
"%C5%8D",
"%C5%BD",
"%8F",
"%C2%90",
"%E2%80%98",
"%E2%80%99",
"%E2%80%9C",
"%E2%80%9D",
"%E2%80%A2",
"%E2%80%93",
"%E2%80%94",
"%CB%9C",
"%E2%84",
"%C5%A1",
"%E2%80",
"%C5%93",
"%9D",
"%C5%BE",
"%C5%B8",
"%C2%A0",
"%C2%A1",
"%C2%A2",
"%C2%A3",
"%C2%A4",
"%C2%A5",
"%C2%A6",
"%C2%A7",
"%C2%A8",
"%C2%A9",
"%C2%AA",
"%C2%AB",
"%C2%AC",
"%C2%AD",
"%C2%AE",
"%C2%AF",
"%C2%B0",
"%C2%B1",
"%C2%B2",
"%C2%B3",
"%C2%B4",
"%C2%B5",
"%C2%B6",
"%C2%B7",
"%C2%B8",
"%C2%B9",
"%C2%BA",
"%C2%BB",
"%C2%BC",
"%C2%BD",
"%C2%BE",
"%C2%BF",
"%C3%80",
"%C3%81",
"%C3%82",
"%C3%83",
"%C3%84",
"%C3%85",
"%C3%86",
"%C3%87",
"%C3%88",
"%C3%89",
"%C3%8A",
"%C3%8B",
"%C3%8C",
"%C3%8D",
"%C3%8E",
"%C3%8F",
"%C3%90",
"%C3%91",
"%C3%92",
"%C3%93",
"%C3%94",
"%C3%95",
"%C3%96",
"%C3%97",
"%C3%98",
"%C3%99",
"%C3%9A",
"%C3%9B",
"%C3%9C",
"%C3%9D",
"%C3%9E",
"%C3%9F",
"%C3%A0",
"%C3%A1",
"%C3%A2",
"%C3%A3",
"%C3%A4",
"%C3%A5",
"%C3%A6",
"%C3%A7",
"%C3%A8",
"%C3%A9",
"%C3%AA",
"%C3%AB",
"%C3%AC",
"%C3%AD",
"%C3%AE",
"%C3%AF",
"%C3%B0",
"%C3%B1",
"%C3%B2",
"%C3%B3",
"%C3%B4",
"%C3%B5",
"%C3%B6",
"%C3%B7",
"%C3%B8",
"%C3%B9",
"%C3%BA",
"%C3%BB",
"%C3%BC",
"%C3%BD",
"%C3%BE",
"%C3%BF"
}

local utfencodeArrayDouble = {
" ",
"%%E2%%82%%AC",
"%%81",
"%%E2%%80%%9A",
"%%C6%%92",
"%%E2%%80%%9E",
"%%E2%%80%%A6",
"%%E2%%80%%A0",
"%%E2%%80%%A1",
"%%CB%%86",
"%%E2%%80%%B0",
"%%C5%%A0",
"%%E2%%80%%B9",
"%%C5%%92",
"%%C5%%8D",
"%%C5%%BD",
"%%8F",
"%%C2%%90",
"%%E2%%80%%98",
"%%E2%%80%%99",
"%%E2%%80%%9C",
"%%E2%%80%%9D",
"%%E2%%80%%A2",
"%%E2%%80%%93",
"%%E2%%80%%94",
"%%CB%%9C",
"%%E2%%84",
"%%C5%%A1",
"%%E2%%80",
"%%C5%%93",
"%%9D",
"%%C5%%BE",
"%%C5%%B8",
"%%C2%%A0",
"%%C2%%A1",
"%%C2%%A2",
"%%C2%%A3",
"%%C2%%A4",
"%%C2%%A5",
"%%C2%%A6",
"%%C2%%A7",
"%%C2%%A8",
"%%C2%%A9",
"%%C2%%AA",
"%%C2%%AB",
"%%C2%%AC",
"%%C2%%AD",
"%%C2%%AE",
"%%C2%%AF",
"%%C2%%B0",
"%%C2%%B1",
"%%C2%%B2",
"%%C2%%B3",
"%%C2%%B4",
"%%C2%%B5",
"%%C2%%B6",
"%%C2%%B7",
"%%C2%%B8",
"%%C2%%B9",
"%%C2%%BA",
"%%C2%%BB",
"%%C2%%BC",
"%%C2%%BD",
"%%C2%%BE",
"%%C2%%BF",
"%%C3%%80",
"%%C3%%81",
"%%C3%%82",
"%%C3%%83",
"%%C3%%84",
"%%C3%%85",
"%%C3%%86",
"%%C3%%87",
"%%C3%%88",
"%%C3%%89",
"%%C3%%8A",
"%%C3%%8B",
"%%C3%%8C",
"%%C3%%8D",
"%%C3%%8E",
"%%C3%%8F",
"%%C3%%90",
"%%C3%%91",
"%%C3%%92",
"%%C3%%93",
"%%C3%%94",
"%%C3%%95",
"%%C3%%96",
"%%C3%%97",
"%%C3%%98",
"%%C3%%99",
"%%C3%%9A",
"%%C3%%9B",
"%%C3%%9C",
"%%C3%%9D",
"%%C3%%9E",
"%%C3%%9F",
"%%C3%%A0",
"%%C3%%A1",
"%%C3%%A2",
"%%C3%%A3",
"%%C3%%A4",
"%%C3%%A5",
"%%C3%%A6",
"%%C3%%A7",
"%%C3%%A8",
"%%C3%%A9",
"%%C3%%AA",
"%%C3%%AB",
"%%C3%%AC",
"%%C3%%AD",
"%%C3%%AE",
"%%C3%%AF",
"%%C3%%B0",
"%%C3%%B1",
"%%C3%%B2",
"%%C3%%B3",
"%%C3%%B4",
"%%C3%%B5",
"%%C3%%B6",
"%%C3%%B7",
"%%C3%%B8",
"%%C3%%B9",
"%%C3%%BA",
"%%C3%%BB",
"%%C3%%BC",
"%%C3%%BD",
"%%C3%%BE",
"%%C3%%BF"
}
alphaBitLib.convertTo1252URLEncode = function(theString)


  theString = alphaBitLib.escape(theString);



  for i=1,#utf8encodeArray do

    local lookfor = utfencodeArrayDouble[i];
    local replacewith = windows1252encodeArrayDouble[i];



    if (lookfor ~= replacewith) then

      theString = string.gsub(theString,lookfor,replacewith);
    end


  end


  return theString

end




[TOPIC: post.html]
#7

solca

[GLOBAL: userInfoPane.html]
solca
  • Enthusiast

  • 53 posts
  • Corona SDK

Thank you very much again for the answers.
 
At the moment I have implemented this solution for clients based on Corona SDK:
 
Bases:
 
a. A string encoded in base 64 does not contain spaces.
 
b. Assuming that in the spaces that the server receives, they actually correspond to the "+" character
 
Solution:
 
In Corona SDK: I encode the string in base64
 
On the server: in the received string I convert the spaces into a "+" character:
 
$myStringToReturn=str_replace(" ","+",$myStringToReturn);
$myStringToReturn=base64_decode($myStringToReturn);
 
Note: I have run tests with images of different sizes (even a 14MB image !) and it works correctly.
 
Regards,
 
Solca



[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: network, request, api, restful, php, b64, image