How to send large POST parameters with URLSession

I am trying to create a class that allows me to send POST requests to a website whose content type is application / x-www-form-urlencoded. The code I am using is as follows:


import Foundation
class HTTPClient {


 func getStringResponse(url: String, method: String, charset: String, parametros: [String: Any], headers: [String: Any])->String{
 var htmlCode = ""

 connectionRequest(url: url, method: method, charset: charset, parametros: parametros, headers: headers, callback: {
  (data, error) in
  htmlCode = data
 })

 while htmlCode == "" {

 }

 return htmlCode
 }


func connectionRequest(url: String, method: String, charset: String, parametros: [String: Any], headers: [String: Any], callback: @escaping (String, String!) -> Void)
 {
  let myUrl = NSURL(string: url)
  let request = NSMutableURLRequest(url: myUrl! as URL)

 request.httpMethod = method

 if method == Constantes.METHOD_POST
 {
  var postString = ""

  for parametro in parametros
  {
  if postString.isEmpty
  {
  var cadena = parametro.key
  cadena.append("=")
  cadena.append(parametro.value as! String)
  postString.append(cadena)
  }
  else
  {
  var cadena = "&"
  cadena.append(parametro.key)
  cadena.append("=")
  cadena.append(parametro.value as! String)
  postString.append(cadena)
  }
  }


  var httpHeaders = [String: String]()
  httpHeaders["Cache-Control"] = "no-cache"
  httpHeaders["Content-Type"] = "application/x-www-form-urlencoded"

  for header in headers
  {
  httpHeaders[header.key] = header.value as! String
  }

  let encodeParameters: Data = postString.data(using: String.Encoding.utf8, allowLossyConversion: false)!
  let decodeParameters = String(data: encodeParameters, encoding: .utf8)

  request.allHTTPHeaderFields=httpHeaders
  request.httpBody = postString.data(using: String.Encoding.utf8, allowLossyConversion: false)

 }


  let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
  if error == nil {
  let htmlCode = String(data: data!, encoding: .ascii)
  callback(htmlCode!, nil)
  return
  }
  }

 task.resume()
}


With this class if the parameter are simple, like "?name=One&surname=Two&city=City", this class works propertly. My problem is for large params. If I try to send next parameters it´s not working.


I can´t add the value of parameters, so I add the link of this question I posted on stackoverflow that include the parma values: https://stackoverflow.com/questions/58474729/how-to-send-large-post-parameters-with-urlsession


In my code, the variable decodeParameters have not the same value than postString after do the encode and decode process. Exists any limitation on the length of this type of parameters?

Answered by Diego Cid in 403637022

I have finally resolved the error. The problem was not in this class, which works perfectly, the problem was that the parameter that was received was not properly scraped to use as a parameter. So I has created this function to encode the parameters before call the class that I posted previously. func encodeParamenters(parametro: String) -> String { let allowedCharacterSet = (CharacterSet(charactersIn: "!*'();:@&=+$,/?%#[] ").inverted) let escapedString = parametro.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet) ?? "" return escapedString }

Reading the referenced post, it seems that your specific issue is that the

decodeParameters
value doesn’t match the original
postString
value. If so, I’m not able to reproduce this. Consider this code snippet:
let postString = … from the referenced post …
let encodeParameters: Data = postString.data(using: String.Encoding.utf8, allowLossyConversion: false)!
let decodeParameters = String(data: encodeParameters, encoding: .utf8)
print(postString == decodeParameters)

On my machine this prints

true
, indicating that the string survived the encode / decode process.

And, just to be super concrete about this, pasted in below is the full source of my test. Here I’ve encoded

postString
value as hex in order to make DevForums happy.

In short, I’m not sure what your actual problem is, but I’m pretty sure that it’s not your encode / decode process.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
import Foundation

let postStringHex = """
    6578636C 75646564 4964504C 6973743D 53746F72 6B26616C 6C6F774C 6567616C
    50657273 6F6E3D74 72756526 53414D4C 52657175 6573743D 50443934 62577767
    646D5679 63326C76 626A3069 4D533477 4969426C 626D4E76 5A476C75 5A7A3069
    56565247 4C546769 507A344B 50484E68 62577779 63447042 6458526F 626C4A6C
    6358566C 63335167 51584E7A 5A584A30 61573975 51323975 63335674 5A584A54
    5A584A32 61574E6C 56564A4D 50534A6F 64485277 637A6F76 4C334E6C 5A47566A
    62474632 5A53356B 5A335175 5A323969 4C6D567A 4F6A5130 4D793958 52554A66
    53566451 557A5666 53553546 56433971 63334176 59323975 59325679 64476C6D
    61574E68 5A473876 6157356B 5A586775 616E4E77 65434967 51323975 63325675
    64443069 64584A75 4F6D3968 63326C7A 4F6D3568 6257567A 4F6E526A 4F6C4E42
    54557736 4D693477 4F6D4E76 626E4E6C 626E5136 6457357A 6347566A 61575A70
    5A575169 4945526C 63335270 626D4630 61573975 50534A6F 64485277 637A6F76
    4C334E6C 4C584268 63324679 5A577868 4C6D4E73 59585A6C 4C6D6476 5969356C
    63793951 636D3934 65533954 5A584A32 61574E6C 55484A76 646D6C6B 5A584969
    49455A76 636D4E6C 51585630 61473439 496D5A68 62484E6C 4969424A 52443069
    58315178 61303155 5A323577 4D6B5533 516B7057 4E334E4D 4F584A4F 596A6C72
    56464E77 64445647 55575670 4969424A 63314268 63334E70 646D5539 496D5A68
    62484E6C 4969424A 63334E31 5A556C75 63335268 626E5139 496A4977 4D546B74
    4D544174 4D6A4255 4D6A4136 4E546B36 4D6A6B75 4D546B33 57694967 55484A76
    6447396A 62327843 6157356B 6157356E 50534A31 636D3436 6232467A 61584D36
    626D4674 5A584D36 64474D36 5530464E 54446F79 4C6A4136 596D6C75 5A476C75
    5A334D36 53465255 55433151 54314E55 49694251 636D3932 6157526C 636B3568
    62575539 496C4579 4F444532 4D44417A 52463946 4D444178 4D7A4131 4D444969
    49465A6C 636E4E70 62323439 496A4975 4D434967 65473173 626E4D36 63335276
    636D7477 50534A31 636D3436 5A585536 63335276 636D7336 626D4674 5A584D36
    64474D36 55315250 556B7336 4D533477 4F6E4279 62335276 59323973 49694234
    62577875 637A707A 64473979 617A3069 64584A75 4F6D5631 4F6E4E30 62334A72
    4F6D3568 6257567A 4F6E526A 4F6C4E55 54314A4C 4F6A4575 4D447068 63334E6C
    636E5270 62323469 49486874 6247357A 4F6E4E68 62577779 63443069 64584A75
    4F6D3968 63326C7A 4F6D3568 6257567A 4F6E526A 4F6C4E42 54557736 4D693477
    4F6E4279 62335276 59323973 49694234 62577875 637A707A 59573173 4D6A3069
    64584A75 4F6D3968 63326C7A 4F6D3568 6257567A 4F6E526A 4F6C4E42 54557736
    4D693477 4F6D467A 63325679 64476C76 6269492B 50484E68 62577779 4F6B6C7A
    6333566C 63694247 62334A74 59585139 496E5679 626A7076 59584E70 637A7075
    5957316C 637A7030 597A7054 5155314D 4F6A4975 4D447075 5957316C 61575174
    5A6D3979 62574630 4F6D5675 64476C30 6553492B 61485230 63446F76 4C314D74
    55455651 5579356E 62335975 65486738 4C334E68 62577779 4F6B6C7A 6333566C
    636A3438 55326C6E 626D4630 64584A6C 49486874 6247357A 50534A6F 64485277
    4F693876 64336433 4C6E637A 4C6D3979 5A793879 4D444177 4C7A4135 4C336874
    6247527A 6157636A 496A344B 50464E70 5A32356C 5A456C75 5A6D382B 43694167
    50454E68 626D3975 61574E68 62476C36 59585270 6232354E 5A58526F 62325167
    5157786E 62334A70 64476874 50534A6F 64485277 4F693876 64336433 4C6E637A
    4C6D3979 5A793879 4D444178 4C7A4577 4C336874 6243316C 65474D74 597A4530
    62694D69 4C7A344B 49434138 55326C6E 626D4630 64584A6C 54575630 6147396B
    49454673 5A323979 6158526F 62543069 61485230 63446F76 4C336433 64793533
    4D793576 636D6376 4D6A4177 4D533877 4E433934 6257786B 63326C6E 4C573176
    636D556A 636E4E68 4C584E6F 59545578 4D694976 50676F67 49447853 5A575A6C
    636D5675 59325567 56564A4A 5053496A 58315178 61303155 5A323577 4D6B5533
    516B7057 4E334E4D 4F584A4F 596A6C72 56464E77 64445647 55575670 496A344B
    49434167 49447855 636D4675 63325A76 636D317A 50676F67 49434167 49434138
    56484A68 626E4E6D 62334A74 49454673 5A323979 6158526F 62543069 61485230
    63446F76 4C336433 64793533 4D793576 636D6376 4D6A4177 4D433877 4F533934
    6257786B 63326C6E 49325675 646D5673 6233426C 5A43317A 61576475 59585231
    636D5569 4C7A344B 49434167 49434167 50465279 5957357A 5A6D3979 62534242
    62476476 636D6C30 61473039 496D6830 64484136 4C793933 64336375 647A4D75
    62334A6E 4C7A4977 4D444576 4D544176 65473173 4C575634 5979316A 4D545275
    49794976 50676F67 49434167 50433955 636D4675 63325A76 636D317A 50676F67
    49434167 50455270 5A32567A 6445316C 64476876 5A434242 62476476 636D6C30
    61473039 496D6830 64484136 4C793933 64336375 647A4D75 62334A6E 4C7A4977
    4D444576 4D445176 65473173 5A57356A 49334E6F 59545578 4D694976 50676F67
    49434167 50455270 5A32567A 64465A68 6248566C 506A5931 516C6C6F 52575A43
    4D6B6832 5A323568 56574658 56564657 59545642 4E6D4A33 59316377 4E31465A
    62565A46 57453531 6233464F 51544E70 65484658 62326435 52557777 54326C4B
    646B5179 63465279 51337069 5246567A 64467032 63455252 5A54526F 59564E33
    4B316435 5A323152 50543038 4C305270 5A32567A 64465A68 6248566C 50676F67
    49447776 556D566D 5A584A6C 626D4E6C 50676F38 4C314E70 5A32356C 5A456C75
    5A6D382B 43694167 49434138 55326C6E 626D4630 64584A6C 566D4673 6457552B
    637A6778 51565A6D 63587072 5A464A73 62455255 51585A43 56566F79 4F446469
    636D3154 57573433 54476448 516D3157 63304A46 53533944 64454A49 6331564F
    54456C57 4C303131 64444634 64455273 6145466B 5457704A 566E5258 4C325579
    526E5A55 63306C46 4D326431 53446449 53316434 546E426D 4F585236 4D56467A
    63584A71 4D555A43 646A6C6F 52307376 54324532 61477730 5A586C79 52323178
    65456C4F 62586478 51306734 63334A35 54584A44 57466377 536A417A 4D6B5A50
    5557316A 5A336F76 596D704C 62485A4A 567A4632 54453542 646E5255 4D574978
    51564678 65564A6F 54466873 4D6B4E73 56455261 5958704D 4D486449 54325130
    61326457 62326C7A 63306B34 61575A73 62327378 4F473034 4E555A44 626A5669
    557A6834 5A30316A 526E426A 646C5A49 5932316F 63303948 656C684D 4E6D4E4C
    4F575653 517A466C 55456776 64334531 4D437458 65576842 63586730 5655646B
    61456F78 65553035 54797452 4F576C52 656C4635 6245347A 54475276 4F487074
    576E4E51 597A5679 4C334A36 516E704F 576D316B 65554674 61575271 55303154
    56545670 5556704B 57437376 62433946 55545255 596E4A73 647A6C43 56316843
    56485276 526B396E 50543038 4C314E70 5A323568 64485679 5A565A68 6248566C
    506A784C 5A586C4A 626D5A76 506A7859 4E544135 52474630 59543438 57445577
    4F554E6C 636E5270 5A6D6C6A 5958526C 506B314A 5355686F 56454E44 516D3079
    5A304633 53554A42 5A306C52 53444E5A 4F484234 65464A71 61455A6A 55486454
    63797379 53477054 5645464F 516D6472 63576872 61556335 647A4243 5156467A
    526B4645 516B684E 55584E33 5131465A 52465A52 55556446 64307047 56587046
    556B3142 4F456442 4D565646 51326433 53564A72 4E553557 517A4654 55544177
    65457055 51577043 5A303557 516B467A 54556846 526B524A 52553532 596C6843
    646D4A74 566E566B 52315A36 53555673 64567074 4F586C69 5930396F 5A456473
    616D497A 54586449 61474E4F 54565272 64303155 52544A4E 56454634 546E7046
    4D6C646F 5930354E 616B6C33 54565246 4D6B3155 5158684F 656B5578 56327044
    516D3536 5255784E 51577448 51544656 52554A6F 54554E53 566B3134 52487042
    546B4A6E 546C5A43 51574E4E 516D7378 516C4A47 536B7053 52455675 54554E56
    52304578 56555644 5A33646C 556B5673 55314A56 546B5254 59303955 56476C43
    53464A56 4E555A56 61305A4E 53555653 526B6C47 556C4E33 4E455A48 5531564F
    55453153 53586446 51566C45 566C4652 526B5633 62464A4E 616D6434 546D7042
    64303077 55586848 52454658 516D644F 566B4A48 52553145 4D567043 566B5657
    56457857 52586C50 52455579 54555242 656C4A45 5257394E 51316C48 51544656
    52554633 64325A52 4D465A54 566B5673 52314E56 546B4A53 5254686E 556B5656
    5A315577 566B3155 5254686E 556B5656 5A314A56 4E565654 56564A43 556B5244
    51304654 53586445 55566C4B 53323961 53576832 59303542 55555643 516C4642
    5247646E 52564242 52454E44 51564676 5132646E 52554A42 546C644F 53315533
    656A5255 576D4A72 5A6B6857 64476474 59316456 4F544E7A 51556C50 4E7A5A5A
    596B5278 5A6E6471 55557072 54544E6B 62316C42 4B306777 52553949 616E5530
    4D6C6C52 556C4244 4D6B7469 55306732 63464646 546A4671 57454A7A 4D7A6C71
    52564273 576D4A74 4B325278 56544E76 4D545A36 57557859 64326735 4E553956
    4F546B30 566D6C71 61455135 57484E36 4F477731 4D314236 54325A4B 597A5250
    647A6459 646E7052 62584E54 4D56526A 54477452 4E303434 65455A4A 55337059
    65437449 56484933 5A6D6772 576D6C45 5A793975 576D5135 567A497A 64474646
    4E486875 596C4A77 62474673 4E306436 59797330 4E6B4A43 656C4E72 53446734
    53576C4D 55335636 4D55567A 546A4A52 616C565A 4E455A4A 526C6C74 4F464277
    4E477036 616E4274 4F466C6E 56324A6E 5A564E32 55477333 5A6B5A78 636C4E61
    626D6B77 4D335634 53453157 576A6852 5A30517A 5A326442 53444533 5A336C45
    4D6B4A54 5A537436 4C334534 65444279 516A4578 517A5978 56446C4A 55476871
    656A4273 526E706E 4E6A6B78 53475653 5A484674 4E334673 516B4A77 52565132
    646A4A56 4D6B314A 5755646D 616B5A48 65553878 57576F31 64584676 6432567A
    51304633 52554642 59553944 516B4A4A 6432646E 5555394E 51586448 51544656
    5A455633 52554976 64314644 54554642 6432645A 52556444 51334E48 51564656
    526B4A33 52554A43 53465633 59337042 4E304A6E 5A334A43 5A305647 516C466A
    6430465A 57585A68 53464977 59305276 646B7779 4F57706A 4D304A71 596A4978
    64307874 546D786A 626C4631 576D3031 64475244 4E57786A 65546C32 57544E4F
    64307777 4F57706A 4D304A54 576C684F 64324979 4E577461 57456C33 546B465A
    53557433 57554A43 55565649 5455464C 52307448 6144426B 53454532 54486B35
    4D32517A 5933565A 4D6C5A35 5A454D31 62574A74 4D54424D 62565A36 54444A4F
    62474E75 556E704D 4D455A45 55544135 546C5644 4E57706A 626C4633 5A326446
    4D454A6E 546C5A49 55304646 5A326446 636B314A 53554A4B 656B4E44 51564A6E
    52304E70 63306442 55564643 636B645A 52454E53 5458646E 5A30564A 54554E72
    52304E44 63306442 55565647 516E644A 516B5A6F 4D57396B 53464A33 54326B34
    646D517A 5A444E4D 62553573 59323552 64567074 4E58526B 517A5673 59336B35
    61324E48 546E704D 656B4E43 4D6D645A 53557433 57554A43 55565649 5157644A
    6432646A 4D45316E 59334245 576C684B 4D474658 576E425A 4D6B5A72 596E6C43
    616D5258 526E4E68 56317077 57544A47 61324A35 516D7461 55304A36 576C6434
    63324A35 516D7869 52315A71 5A45684D 52484D79 4E58425A 4D6A686E 597A4A57
    626E6333 6348564A 53457073 576A4A34 61474A58 566E566B 527A686E 576C6857
    6557497A 516D7869 65554A73 55315653 516C5635 4E476456 4D315A78 576C6853
    646B6C48 52576469 52305A36 5355644F 646D4A74 556E425A 4D6D7832 596D3157
    656B6C48 556D784A 53465A36 596E6C43 62475649 516A4661 57453477 5756684E
    5A317058 4E476469 5230566E 556B5A43 52456C48 556D784A 52567050 56465A52
    64465672 546B354A 52303532 596D6C43 54314E56 57545A4A 526B5635 5430524A
    4D6B3145 5154424D 5657396E 5330564E 646C4E74 4F586C61 4D6C566E 55323557
    61474A70 5158684E 52466C30 5457706E 64303145 61335255 56305A72 59323173
    61307856 566E706A 52306845 63316446 63453142 61306443 64314642 61537434
    51554652 52586451 55566C45 566C4977 556B4A45 5758644F 53314635 54555242
    65457871 51584E43 5A327479 516D6446 52554668 65473142 5557644E 5344424F
    526C5673 556B7053 61327845 55565653 55456C46 556B5A4A 526B3547 56455634
    55456C46 556B5A4A 52565A50 566B5673 52564656 55586449 55566C45 566C4977
    62454A43 57586447 51566C4A 5333645A 516B4A52 56556842 64306C48 51304E7A
    52304652 56555A43 64303146 54554530 52304578 56575245 64305643 4C336452
    52554633 53555534 5245466B 516D644F 566B6852 4E455647 5A314656 63555273
    4E486833 616D746E 4E544656 4E585978 56584668 59304535 4D323550 6255314E
    64326469 51556444 51334E48 51564656 526B4A33 52555243 53556471 54556C48
    5A303142 5A306443 5A314642 616D745A 516B4655 51557843 5A316C46 51556B31
    52304652 54554E42 55546833 5258645A 52304A42 51303953 5A305648 54554672
    52304A33 55554671 61316C43 516D644A 64324E6E 57556443 51554E50 556D6446
    526B3148 5A33644E 61466C7A 59556853 4D474E49 54545A4D 65546B7A 5A444E6A
    64566B79 566E6C6B 517A5674 596D3078 4D457874 566E704D 4D304A72 59336B35
    55564A47 546D5A52 4D446C4F 56555935 62474E35 4E586461 52316C55 51573157
    656B3145 5356644D 52326777 5A456843 656B3970 4F485A6B 4D32517A 5447314F
    62474E75 55585661 62545630 5A454D31 62474E35 4F586461 53453132 56555653
    56466777 546C4255 566B4A6D 576C6330 64574E48 556D3146 64307073 596D7042
    5A6B4A6E 546C5A49 55303146 52305242 56326443 55566F72 526D6432 526B3568
    6258704B 63305674 5157644F 5645356C 636B464C 5A555261 56454E43 4E45465A
    52465A53 4D475A43 5355685A 54556C49 566B314A 53464E76 53556851 62306C49
    54576876 52325669 52314A6F 59305276 646B7779 6547745A 57454A71 596A4978
    64307874 546D786A 626C4631 576D3031 64475244 4E57786A 65546C45 56476F78
    52465672 6433684D 52546C57 55465647 52457055 53586452 4D6A6C30 59306335
    64567058 4E544261 57453173 54577043 536D4A74 576E5A6A 6254466F 5A456473
    616D497A 54584E55 656A4648 56477378 56557857 536B5255 55336845 55465657
    56464179 546D786A 626C4A77 576D3173 616C6C59 556D7856 62565979 596A4A4F
    61475248 62485A69 61336877 597A4E52 4E316C74 6248565A 57456F31 55444A4B
    61474D79 56533969 4D6B7078 576C644F 4D466B79 6547686A 4D303035 5754464B
    54564A48 6248706B 53457077 57573557 4D474658 4F585656 527A6C77 596D3554
    52307458 6144426B 53454532 54486B35 4D32517A 5933565A 4D6C5A35 5A454D31
    62574A74 4D54424D 62565A36 54444A4F 65574A49 546D7069 4D6A4633 5444424F
    55315245 5258565A 4D30707A 54554577 52304E54 63556454 5357497A 52464646
    516B4E33 56554642 4E456C43 51564643 56565A79 576D4D34 5158426D 62307868
    4F445233 536A5677 4F58466D 62585250 5646686D 63314E50 4E6D5243 626E4672
    64555979 64466C6E 52573948 62544E75 4F58564E 526C5A7A 57586C61 51587076
    5A6B3969 4D464534 51584A6D 62574645 4D565932 65566454 57464673 4D585669
    4E57737A 5758426B 55323946 59315277 4C314669 55566B78 52304661 517A5273
    4D6A6842 4D553561 56455572 6144686E 54556F78 54797434 4D6D7442 53554A4F
    536C6861 54445978 59306331 59554A6A 4E6B7846 53555650 4E575A52 556B7045
    55473971 54454A49 546D5243 4F553079 596A4A54 536B5234 546A5A33 6447786B
    4E557873 52575578 576E4669 61304E5A 6447347A 547A5A52 65573533 55544972
    4F553172 5A6D3432 617A6C6A 4C306772 53574A4A 5A437434 57455131 516E4A30
    63697449 5A6A4252 63327434 4C325A71 64584678 53455A32 59314E45 5A6D4A59
    62566C74 54316448 516D5674 53307070 63457453 4D576B31 62464A47 4D6D356F
    53475255 566D6C4E 4E537449 51556433 62305976 4E336332 536C5242 4F465235
    65584D31 626A4A4D 4E474A7A 4B335242 51315A32 59565678 4F55566A 65577069
    4E693946 4E6A526C 556C4A58 536D7848 5A565246 4E6E6338 4C316731 4D446C44
    5A584A30 61575A70 59324630 5A543438 57445577 4F556C7A 6333566C 636C4E6C
    636D6C68 62443438 57445577 4F556C7A 6333566C 636B3568 6257552B 54315539
    496B4644 49454E76 62584276 626D5675 6447567A 49456C75 5A6D3979 62567844
    4D317842 4D585270 5932397A 49697767 547A3147 546B3155 4C564A44 54537767
    517A3146 557A7776 57445577 4F556C7A 6333566C 636B3568 6257552B 50466731
    4D446C54 5A584A70 5957784F 64573169 5A58492B 4E444534 4D546B35 4F446B77
    4F444D77 4F444D77 4D7A5134 4E6A5579 4D6A4D78 4D7A4933 4D7A6732 4D444D34
    4D7A5979 4D7A4D38 4C316731 4D446C54 5A584A70 5957784F 64573169 5A58492B
    50433959 4E544135 53584E7A 64575679 55325679 61574673 506A7776 57445577
    4F555268 6447452B 5043394C 5A586C4A 626D5A76 506A7776 55326C6E 626D4630
    64584A6C 506A787A 59573173 4D6E4136 52586830 5A57357A 61573975 637A3438
    63335276 636D7336 55585668 62476C30 65554631 6447686C 626E5270 59324630
    61573975 51584E7A 64584A68 626D4E6C 54475632 5A57772B 4D6A7776 63335276
    636D7336 55585668 62476C30 65554631 6447686C 626E5270 59324630 61573975
    51584E7A 64584A68 626D4E6C 54475632 5A57772B 50484E30 62334A72 4F6E4E77
    5532566A 64473979 506B5246 54553874 55314138 4C334E30 62334A72 4F6E4E77
    5532566A 64473979 506A787A 64473979 617A707A 63456C75 63335270 64485630
    61573975 506B5246 54553874 55314138 4C334E30 62334A72 4F6E4E77 5357357A
    64476C30 64585270 6232342B 50484E30 62334A72 4F6E4E77 51584277 62476C6A
    59585270 6232342B 63484E68 4D547776 63335276 636D7336 63334242 63484273
    61574E68 64476C76 626A3438 63335276 636D7477 4F6D564A 52464E6C 59335276
    636C4E6F 59584A6C 506E5279 64575538 4C334E30 62334A72 6344706C 53555254
    5A574E30 62334A54 61474679 5A543438 63335276 636D7477 4F6D564A 52454E79
    62334E7A 5532566A 64473979 55326868 636D552B 64484A31 5A547776 63335276
    636D7477 4F6D564A 52454E79 62334E7A 5532566A 64473979 55326868 636D552B
    50484E30 62334A72 6344706C 53555244 636D397A 63304A76 636D526C 636C4E6F
    59584A6C 506E5279 64575538 4C334E30 62334A72 6344706C 53555244 636D397A
    63304A76 636D526C 636C4E6F 59584A6C 506A787A 64473979 61334136 556D5678
    6457567A 6447566B 51585230 636D6C69 6458526C 637A3438 63335276 636D7336
    556D5678 6457567A 6447566B 51585230 636D6C69 6458526C 49453568 62575539
    496D6830 64484136 4C793933 64336375 63335276 636D7375 5A323932 4C6D5631
    4C7A4575 4D43396C 5357526C 626E5270 5A6D6C6C 63694967 61584E53 5A584631
    61584A6C 5A443069 64484A31 5A534967 546D4674 5A555A76 636D3168 64443069
    4969382B 50484E30 62334A72 4F6C4A6C 6358566C 6333526C 5A454630 64484A70
    596E5630 5A53424F 5957316C 50534A6F 64485277 4F693876 64336433 4C6E4E30
    62334A72 4C6D6476 6469356C 64533878 4C6A4176 5A326C32 5A57354F 5957316C
    49694270 63314A6C 63585670 636D566B 50534A30 636E566C 4969424F 5957316C
    526D3979 62574630 50534969 4C7A3438 63335276 636D7336 556D5678 6457567A
    6447566B 51585230 636D6C69 6458526C 49453568 62575539 496D6830 64484136
    4C793933 64336375 63335276 636D7375 5A323932 4C6D5631 4C7A4575 4D43397A
    64584A75 5957316C 49694270 63314A6C 63585670 636D566B 50534A6D 5957787A
    5A534967 546D4674 5A555A76 636D3168 64443069 4969382B 50484E30 62334A72
    4F6C4A6C 6358566C 6333526C 5A454630 64484A70 596E5630 5A53424F 5957316C
    50534A6F 64485277 4F693876 64336433 4C6E4E30 62334A72 4C6D6476 6469356C
    64533878 4C6A4176 6157356F 5A584A70 6447566B 526D4674 61577835 546D4674
    5A534967 61584E53 5A584631 61584A6C 5A443069 5A6D4673 63325569 49453568
    62575647 62334A74 59585139 49694976 506A787A 64473979 617A7053 5A584631
    5A584E30 5A575242 64485279 61574A31 64475567 546D4674 5A543069 61485230
    63446F76 4C336433 6479357A 64473979 6179356E 62335975 5A585576 4D533477
    4C326C7A 5A473570 5A534967 61584E53 5A584631 61584A6C 5A443069 5A6D4673
    63325569 49453568 62575647 62334A74 59585139 49694976 506A787A 64473979
    617A7053 5A584631 5A584E30 5A575242 64485279 61574A31 64475567 546D4674
    5A543069 61485230 63446F76 4C336433 6479357A 64473979 6179356E 62335975
    5A585576 4D533477 4C334A6C 5A326C7A 64475679 56486C77 5A534967 61584E53
    5A584631 61584A6C 5A443069 64484A31 5A534967 546D4674 5A555A76 636D3168
    64443069 4969382B 50484E30 62334A72 4F6C4A6C 6358566C 6333526C 5A454630
    64484A70 596E5630 5A53424F 5957316C 50534A6F 64485277 4F693876 64336433
    4C6E4E30 62334A72 4C6D6476 6469356C 64533878 4C6A4176 59326C30 6158706C
    626C4642 5155786C 646D5673 49694270 63314A6C 63585670 636D566B 50534A30
    636E566C 4969424F 5957316C 526D3979 62574630 50534969 4C7A3438 63335276
    636D7336 556D5678 6457567A 6447566B 51585230 636D6C69 6458526C 49453568
    62575539 496D6830 64484136 4C793933 64336375 63335276 636D7375 5A323932
    4C6D5631 4C7A4575 4D433968 5A6D6C79 62574653 5A584E77 6232357A 5A534967
    61584E53 5A584631 61584A6C 5A443069 5A6D4673 63325569 49453568 62575647
    62334A74 59585139 49694976 506A787A 64473979 617A7053 5A584631 5A584E30
    5A575242 64485279 61574A31 64475567 546D4674 5A543069 61485230 63446F76
    4C336433 6479357A 64473979 6179356E 62335975 5A585576 4D533477 4C323568
    64476C76 626D4673 61585235 5132396B 5A534967 61584E53 5A584631 61584A6C
    5A443069 5A6D4673 63325569 49453568 62575647 62334A74 59585139 49694976
    506A7776 63335276 636D7477 4F6C4A6C 6358566C 6333526C 5A454630 64484A70
    596E5630 5A584D2B 50484E30 62334A72 63447042 6458526F 5A573530 61574E68
    64476C76 626B4630 64484A70 596E5630 5A584D2B 50484E30 62334A72 63447057
    53555251 51585630 61475675 64476C6A 59585270 62323542 64485279 61574A31
    6447567A 506A787A 64473979 61334136 5531424A 626D5A76 636D3168 64476C76
    626A3438 63335276 636D7477 4F6C4E51 5355512B 5245564E 54793154 55447776
    63335276 636D7477 4F6C4E51 5355512B 5043397A 64473979 61334136 5531424A
    626D5A76 636D3168 64476C76 626A3438 4C334E30 62334A72 63447057 53555251
    51585630 61475675 64476C6A 59585270 62323542 64485279 61574A31 6447567A
    506A7776 63335276 636D7477 4F6B4631 6447686C 626E5270 59324630 61573975
    51585230 636D6C69 6458526C 637A3438 4C334E68 62577779 63447046 6548526C
    626E4E70 6232357A 506A7776 63324674 62444A77 4F6B4631 64476875 556D5678
    6457567A 6444343D 26666F72 63656449 64503D6E 6F6E65
    """

extension String {

    func cluster(maxCount: Int) -> AnySequence<Substring> {
        return AnySequence(sequence(state: self.startIndex) { index -> Substring? in
            guard index != self.endIndex else {
                return nil
            }
            let base = index
            _ = self.formIndex(&index, offsetBy: maxCount, limitedBy: self.endIndex)
            return self[base..<index]
        })
    }
}

// let postStringOrig = … from the referenced post …
let postStringBytes = postStringHex
    .filter({!$0.isWhitespace})
    .cluster(maxCount: 2)
    .map { s -> UInt8 in
        precondition(s.count == 2)
        return UInt8(s, radix: 16)!
    }
let postString = String(bytes: postStringBytes, encoding: .utf8)!
// assert(postString == postStringOrig)
let encodeParameters: Data = postString.data(using: String.Encoding.utf8, allowLossyConversion: false)!
let decodeParameters = String(data: encodeParameters, encoding: .utf8)
print(postString == decodeParameters)

Thank you very much for your answer. My main problem is that making the call to the web with the parameters of the form is returning an error due to incorrect parameters. As I commented in my post, other websites with simpler parameters are correctly configured, so he began to distrust the passing of parameters due to its length. What does not fit me is how the check is returning you a true one, when reviewing the values at runtime are the ones I have put in the related post. The truth is that I am a bit frustrated with the issue because it is something that should work without a problem and I do not understand how it could be causing problems.

My main problem is that making the call to the web with the parameters of the form is returning an error due to incorrect parameters.

I strongly suspect that the parameters are not making it on the ‘wire’ the way that you think they are. I wrote up my some general advice on this topic in Debugging HTTP Server-Side Errors.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi have review your post, and I have checked that the response code was 200, but in the body (html) there was a text that indicates that there was an error of validation. So my question now is if it´s possible that the system have sending the request two times. In this case of validation, the parameters only are valids one time.

[is it] possible that the system have sending the request two times. In this case of validation, the parameters only are valids one time.

Certainly. There are various circumstances under which CFNetwork will retransmit requests. It shouldn’t retransmit a POST request (the HTTP RFCs make it very clear that POSTs are not idempotent) unless it’s sure that no part of the request made it to the server, but it’s always possible that something has gone wrong with that limiter.

If you’re concerned about this, you can rule it out using various debugging tools. Probably the easiest would be CFNetwork diagnostic logging.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer

I have finally resolved the error. The problem was not in this class, which works perfectly, the problem was that the parameter that was received was not properly scraped to use as a parameter. So I has created this function to encode the parameters before call the class that I posted previously. func encodeParamenters(parametro: String) -> String { let allowedCharacterSet = (CharacterSet(charactersIn: "!*'();:@&=+$,/?%#[] ").inverted) let escapedString = parametro.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet) ?? "" return escapedString }

How to send large POST parameters with URLSession
 
 
Q