Problems sending XML string Response via HTML POST

Me again today… :slight_smile:

I built a web server that answers to a form sending IOS app called “Formentry”.

I can’t figure out why the app crashes when the XML response I send comes from a xmlDocument.ToString. If I use a constant to store the request’s response then it works fine.

Exemple :

My XML response looks like that :

<?xml version="1.0" encoding="UTF-8"?>
id nom nip
1 Roger 1606
2 Claude 1234
3 Rose 4567

If you prefer…

<?xml version="1.0" encoding="UTF-8"?> ....
id nom nip
1 Roger 1606

Code to send response
inspveh() is the method creating the XML response it returns a xmldocument
constant is a string constant containing the same XML as the one produced by inspveh()

Using inspveh() : FAIL The app crashes

Dim strXml As String strXml = DefineEncoding(inspvehImport(Request).ToString, Encodings.UTF8) Request.Header("Content-Type") ="text/html; charset=utf-8" Request.Print(strXml) Return True )

Using constant : SUCCESS the internal database is updated

Request.Header("Content-Type") = "text/html; charset=utf-8 Request.Print(constant) Return True )

Thanks!!!

I forgot to say that we have a PHP little program that actually does the job but we need to build new PHP files for any new form we create. Since I’m not familiar with PHP, I’d rather have a webserver made with Xojo…

Thanks again!

Dim strXml As String is local and maybe out of scope ?
have you try it with Global property?

I assume the app crashes with an XMLException. If so, you’re doing something wrong with the XMLDocument. Break that part out into a separate project and test it, so you can see what’s happening.

Loannis: that’s just silly. The variable is used immediately and within scope. And even so, it couldn’t cause a crash.

Thanks Tim,

You are right, the strXml is not out of scope and is apparently identical to the constant. If I paste both of them side by side, there is no difference. The only difference between them is that one is contained in a constant and the other is generated by a method that returns an XMLDocument from which I extract the content by using “.to string”. I really can’t figure it out…

Did you examine them byte by byte? There could be non-printable characters embedded in it. And so you’re saying that the crash is not an xmlexception? Then what is it?

Thanks for suppoprting me… I’ve been working on that problem for a couple of days now …

I did not went through bite by bite but I tried “Encodings.UTF8.IsValidData(strXML)” and it returned True.

Instead of using my method to generate the XML String, I hard coded it.

      strXml = DefineEncoding("<?xml version=" + Chr(34) + "1.0"+ Chr(34) + " encoding="+ Chr(34) + "UTF-8" + Chr(34) + "?>" + _
      "<database><table id="+ Chr(34) + "employes" + Chr(34) + " " + Chr(34) + "updated=" + Chr(34) + "2016-05-14 15:32:00 +0000" + Chr(34) + ">" + _
      "<thead><tr><th type=" + Chr(34) + "id" + Chr(34) + ">id</th><th type=" + Chr(34) + "string" + Chr(34) + ">nom</th><th type=" + Chr(34) + "string" + Chr(34) + ">nip</th>" + _
      "</tr></thead><tbody><tr><td>1</td><td>Roger</td><td>1606</td></tr><tr><td>2</td><td>Claude</td><td>1234</td></tr><tr><td>3</td><td>Rose</td><td>4567</td></tr>" + _
      "</tbody></table></database>", Encodings.UTF8)

It doesn’t crash anymore but still doesn’t updates the app’s internal database…

And, I agree with you about the fact that it could be an exception that was causing the crash…

But I can’t find why…
1- Sending a constant string value works
2- Sending a string variable doesn’t work but doesn’t make the app crash
3- Sending XMLDocument.toString crashes the app…

What might be different between these?

Have you run it in the debugger to see whether it’s an exception or a flat out crash? The debugger will break on an exception, or at least display a message.

And I believe that IF you have the same string, it should not make any difference whether it is a constant or a string variable. You can test that by simple setting your string variable to the constant and returning that.

Dim strXml As String
strXml = constant
Request.Header("Content-Type") = "text/html; charset=utf-8
Request.Print(strXml)
Return True

[quote=265400:@Roger St-Arneault]And, I agree with you about the fact that it could be an exception that was causing the crash…

But I can’t find why…
1- Sending a constant string value works
2- Sending a string variable doesn’t work but doesn’t make the app crash
3- Sending XMLDocument.toString crashes the app…

What might be different between these?[/quote]
would need to compare them byte by byte but there HAS to be some differences

I’ll do a byte by byte check and I’ll try couple of other things I have in mind. I’ll let you know what I get …

Thanks a lot for your time and advices!

Still on that… Tried something else but got the same result… The response taken from the constant works and the one from xmlDocument.print transferred to the strXML variable doesn’t.

The two captures show the binary version of the two Request responses. The text version is the following :

<?xml version="1.0" encoding="UTF-8"?>

id nom nip
1 Roger 1606
2 Claude 1234
3 Justin 4567

As you can see, they are both the same. The only odd thing is the dot added to the date (indicated by the arrow) which is not in the text version. What is it? It doesn’t seem to matter since it is present in both the constant and the strXML variable.
Is that a valid way to compare those two strings?

What is going on here?

thats a space and appears to be in both

Ok, that’s what I thought…

I followed Tim’s advice :

[quote=265402:@Tim Hare]Dim strXml As String
strXml = constant
Request.Header(“Content-Type”) = "text/html; charset=utf-8
Request.Print(strXml)
Return True[/quote]

and it worked… I don’t know what to conclude… The XML produced by my method seems to be responsible for the problem… But how? When I copy the text result in the constant it works but when I take it directly from XmlDocument.ToString, it doesn’t…

without seeing how your method creates the XML its hard to say

Correction, the result in a constant doesn’t work either…
Here is the method with french annotations …


//Paramtres
  Dim dTableName_1 As String = "employes" //Nom de la table de destination des donnes
  
  //Vrifie connexion  la BD
  
  Dim db As New PostgreSQLDatabase
  db.Host = "xx.xx.xx.xx"
  db.Port = 5432
  db.DatabaseName = "webservertest"
  db.UserName = "xxxxxxxx"
  db.Password = "xxxxxxxx"
  
  If db.Connect Then
    
    //Charger les donnes requises : employs
    Dim strSQL As String = "SELECT * FROM " + dTableName_1
    Dim pgps As PostgreSQLPreparedStatement = db.Prepare(strSQL)
    Dim RS As RecordSet = pgps.SQLSelect
    
    //Gnrer le fichier XML
    Dim xmlDataDoc As New XmlDocument
    
    //Balise Database
    Dim root As XmlNode
    root = xmlDataDoc.AppendChild(xmlDataDoc.CreateElement("database"))
    
    //*******Table employs
    Using Xojo.Core
    Dim d As xojo.Core.Date = xojo.Core.Date.Now
    Dim gmt As New xojo.Core.TimeZone(0)
    Dim gmtDate As New xojo.Core.Date(d.SecondsFrom1970, gmt)
    Dim di As New DateInterval
    di.Days = 1
    Dim updatedDate As xojo.Core.Date = gmtDate + di
    
    Dim table As XmlNode
    table = root.AppendChild(xmlDataDoc.CreateElement("table"))
    table.SetAttribute("id", dTableName_1)
    table.SetAttribute("updated", updatedDate.ToText + " +0000")
    
    
    //Titres de colonnes
    Dim head As XmlNode
    head = table.AppendChild(xmlDataDoc.CreateElement("thead"))
    //Crer un rang
    Dim rang As XmlNode
    rang = head.AppendChild(xmlDataDoc.CreateElement("tr"))
    //Titres des colonnes
    Dim titre1 As XmlNode //Titre 1
    titre1 = rang.AppendChild(xmlDataDoc.CreateElement("th"))
    titre1.SetAttribute("type", "id")
    titre1.AppendChild(xmlDataDoc.CreateTextNode("id"))
    Dim titre2 As XmlNode //Titre 2
    titre2 = rang.AppendChild(xmlDataDoc.CreateElement("th"))
    titre2.SetAttribute("type", "String")
    titre2.AppendChild(xmlDataDoc.CreateTextNode("nom"))
    Dim titre3 As XmlNode //Titre 3
    titre3 = rang.AppendChild(xmlDataDoc.CreateElement("th"))
    titre3.SetAttribute("type", "String")
    titre3.AppendChild(xmlDataDoc.CreateTextNode("nip"))
    
    //Donnes
    Dim body As XmlNode
    body = table.AppendChild(xmlDataDoc.CreateElement("tbody"))
    If RS <> Nil And Not (RS.BOF And RS.EOF) Then
      While Not RS.EOF
        Dim rangData As XmlNode
        rangData = body.AppendChild(xmlDataDoc.CreateElement("tr"))
        Dim data1 As XmlNode //Donne 1
        data1 = rangData.AppendChild(xmlDataDoc.CreateElement("td"))
        data1.AppendChild(xmlDataDoc.CreateTextNode(RS.Field("id").StringValue))
        Dim data2 As XmlNode //Donne 2
        data2 = rangData.AppendChild(xmlDataDoc.CreateElement("td"))
        data2.AppendChild(xmlDataDoc.CreateTextNode(RS.Field("nom").StringValue))
        Dim data3 As XmlNode //Donne 3
        data3 = rangData.AppendChild(xmlDataDoc.CreateElement("td"))
        data3.AppendChild(xmlDataDoc.CreateTextNode(RS.Field("nip").StringValue))
        
        RS.MoveNext
      Wend
      
      Return xmlDataDoc
    End If
  Else
    Return Nil
  End If

As for cars, the problem is often behind the wheel… :slight_smile:

Thanks for taking a look at it!

Does that mean neither works then ?

Maybe I have a clue… I used Paw to call the PHP file that prepares the xml string. I copied the Request response (text format) in the constant and it works… Now I remember that’s how I made it work.

Here is the PHP result :

<?xml version="1.0" encoding="UTF-8"?>
<database>  <table id="employes" updated="2016-05-13 15:21:19 +0000">
       <thead>
          <tr>
              <th type="id">id</th>
              <th type="string">nom</th>
              <th type="string">nip</th>
          </tr>
       </thead>
       <tbody>
          <tr>
              <td>1</td>
              <td>Roger</td>
              <td>1606</td>
          </tr>
          <tr>
              <td>2</td>
              <td>Claude</td>
              <td>1234</td>
          </tr>
          <tr>
              <td>3</td>
              <td>Justin</td>
              <td>4567</td>
          </tr>
      </tbody>
  </table>
</database>

And here is the XML generated by my code

<?xml version="1.0" encoding="UTF-8"?><database><table id="employes" updated="2016-05-13 19:30:29 +0000"><thead><tr><th type="id">id</th><th type="String">nom</th><th type="String">nip</th></tr></thead><tbody><tr><td>1</td><td>Roger</td><td>1606</td></tr><tr><td>2</td><td>Claude</td><td>1234</td></tr><tr><td>3</td><td>Justin</td><td>4567</td></tr></tbody></table></database>

Besides the “presentation” I can’t see the difference… Besides the uppercases I use for the String types…

I replaced the uppercases and it works… Sorry ! I think I will erase that post… I’m a bit ashamed… :slight_smile:

Thanks! and sorry for waisting your time…