Trying to figure out why my HTTPRequest Post does not work

Just starting out? Need help? Post your questions and find answers here.
Steving
User
User
Posts: 13
Joined: Tue Sep 12, 2017 11:40 pm
Location: San Francisco Bay Area
Contact:

Trying to figure out why my HTTPRequest Post does not work

Post by Steving »

Hello all,

I'm trying to get some data uploaded to a server run by a 3rd party, and I was having issues, do I broke it down to it's most basic, and was trying to work with the dneonline calculator through a SOAP request. Here is my code:

Code: Select all

NewMap Header.s()
Header("POST") = "http://www.dneonline.com/calculator.asmx HTTP/1.1"
Header("Accept-Encoding:") = "gzip,deflate"
Header("Content-Type:") = "text/xml;charset=UTF-8"
Header("SOAPAction:") = #DQUOTE$ + "http://tempuri.org/Add" + #DQUOTE$
Header("Host:") = "www.dneonline.com"
Header("Proxy-Connection:") = "Keep-Alive"
Header("User-Agent:") = "Apache-HttpClient/4.5.5 (Java/12.0.1)"

If Not InitNetwork()
  MessageRequester("Help!","Unable to initialize network!")
  End 100
EndIf

Soap.s = "<soapenv:Envelope xmlns:soapenv=" + #DQUOTE$ + "http://schemas.xmlsoap.org/soap/envelope/" + #DQUOTE$ + " xmlns:tem=" + #DQUOTE$ + "http://tempuri.org/" + #DQUOTE$ + ">" +
   "<soapenv:Header/>" +
   "<soapenv:Body>" +
      "<tem:Add>" +
         "<tem:intA>5</tem:intA>" +
         "<tem:intB>3</tem:intB>" +
      "</tem:Add>" +
   "</soapenv:Body>" +
"</soapenv:Envelope>"

; Header("Content-Length:") = Len(Soap)

PostString.s = "http://www.dneonline.com/calculator.asmx"

Debug "About to send the request"
ForEach Header()
  Debug MapKey(Header()) + ": " + Header()
Next

Debug "-------------------------------------"
Debug Soap
Debug "-------------------------------------"

Running = HTTPRequest(#PB_HTTP_Post, PostString, Soap, 0, Header())
If Running
  Debug "Status Code: " + HTTPInfo(Running, #PB_HTTP_StatusCode)
  FullResponse.s = HTTPInfo(Running, #PB_HTTP_Response)
  Debug " "
  Debug FullResponse
  FinishHTTP(Running)
  Debug FindString(FullResponse, "name=" + #DQUOTE$ + "F9errCode")
  ResponseCode.s = Mid(FullResponse, FindString(FullResponse,"<AddResult>")+11)
  ResponseCode = Left(ResponseCode, FindString(ResponseCode, "</AddResult>")-1)
Else
  Debug "Woa, you gotsa sometin wrong..."
  Debug Running
EndIf
Here is the debug output I get:
Obviously, some of that is the things that I have printed through my own code, but here's the interesting part, when I take the stuff between the two lines (starting with <soapenv... ) and paste it into SoapUI, it works just fine! I actually am modelling this post request after one that I created in SoapUI so that I can make sure that I'm doing it right. There has to be something up with my headers, but I've been changing them around and commenting them out but it only seems to make it worse. I would really appreciate some direction here. This is just a small program to try and narrow down the problem with my SOAP syntax, this is not by any means a program I'm using in production so I apologize for the potentially sloppy code.

Thanks in advance for your time.

Stevinga
infratec
Always Here
Always Here
Posts: 7588
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Trying to figure out why my HTTPRequest Post does not wo

Post by infratec »

Code: Select all

NewMap Header.s()
Header("Accept-Encoding") = "gzip,deflate"
Header("Content-Type") = "text/xml;charset=UTF-8"
Header("SOAPAction") = #DQUOTE$ + "http://tempuri.org/Add" + #DQUOTE$
Header("Host") = "www.dneonline.com"
Header("Proxy-Connection") = "Keep-Alive"
;Header("User-Agent:") = "Apache-HttpClient/4.5.5 (Java/12.0.1)"

If Not InitNetwork()
  MessageRequester("Help!","Unable to initialize network!")
  End 100
EndIf

Soap$ = "<soapenv:Envelope xmlns:soapenv=" + #DQUOTE$ + "http://schemas.xmlsoap.org/soap/envelope/" + #DQUOTE$ + " xmlns:tem=" + #DQUOTE$ + "http://tempuri.org/" + #DQUOTE$ + ">" +
         "<soapenv:Header/>" +
         "<soapenv:Body>" +
         "<tem:Add>" +
         "<tem:intA>5</tem:intA>" +
         "<tem:intB>3</tem:intB>" +
         "</tem:Add>" +
         "</soapenv:Body>" +
         "</soapenv:Envelope>"

; Header("Content-Length:") = Len(Soap)

URL$ = "http://www.dneonline.com/calculator.asmx"

Debug "About to send the request"
ForEach Header()
  Debug MapKey(Header()) + ": " + Header()
Next

Debug "-------------------------------------"
Debug Soap
Debug "-------------------------------------"

Running = HTTPRequest(#PB_HTTP_Post, URL$, Soap$, 0, Header())
If Running
  Debug "Status Code: " + HTTPInfo(Running, #PB_HTTP_StatusCode)
  FullResponse.s = HTTPInfo(Running, #PB_HTTP_Response)
  Debug " "
  Debug FullResponse
  FinishHTTP(Running)
  Debug FindString(FullResponse, "name=" + #DQUOTE$ + "F9errCode")
  ResponseCode.s = Mid(FullResponse, FindString(FullResponse,"<AddResult>")+11)
  ResponseCode = Left(ResponseCode, FindString(ResponseCode, "</AddResult>")-1)
Else
  Debug "Woa, you gotsa sometin wrong..."
  Debug Running
EndIf
To get a readable answer:

Code: Select all

NewMap Header.s()
;Header("Accept-Encoding") = "gzip,deflate"
Header("Content-Type") = "text/xml;charset=UTF-8"
Header("SOAPAction") = #DQUOTE$ + "http://tempuri.org/Add" + #DQUOTE$
;Header("Host") = "www.dneonline.com"
;Header("Proxy-Connection") = "Keep-Alive"
;Header("User-Agent:") = "Apache-HttpClient/4.5.5 (Java/12.0.1)"
Sending the content length is a good idea, but you need:

Code: Select all

Header("Content-Length") = Str(StringByteLength(Soap$, #PB_UTF8))
User avatar
TI-994A
Addict
Addict
Posts: 2705
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Trying to figure out why my HTTPRequest Post does not wo

Post by TI-994A »

Here's one, working off Bernd's code and some examples from the manual, utilising the XML parsing functions to display the request and the response in tree-views:

Code: Select all

Procedure populateTree(*node, level, tree)
  
  If XMLNodeType(*node) = #PB_XML_Normal
    nodeText.s = GetXMLNodeName(*node) + ": "
    
    If ExamineXMLAttributes(*node)
      While NextXMLAttribute(*node)
        nodeText + " Attribute:" + XMLAttributeName(*node) + 
                   ~"=\"" + XMLAttributeValue(*node) + ~"\" "
      Wend
    EndIf
    
    nodeText + " " + GetXMLNodeText(*node)
    AddGadgetItem(tree, -1, nodeText, 0, level)
    *childNode = ChildXMLNode(*node)
    
    While *childNode 
      populateTree(*childNode, level + 1, tree)      
      *childNode = NextXMLNode(*childNode)      
    Wend        
    
  EndIf
  
EndProcedure

Procedure displayXML(xml, tree)
  
  *node = MainXMLNode(xml)      
  If *node
    populateTree(*node, 0, tree)
    For i = 0 To CountGadgetItems(tree) - 1
      SetGadgetItemState(tree, i, #PB_Tree_Expanded)
    Next i
  EndIf     
  
EndProcedure

If InitNetwork() 
  
  wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered 
  OpenWindow(0, 0, 0, 500, 700, "HTTPRequest/XML", wFlags)
  TextGadget(#PB_Any, 10, 10, 100, 20, "HTTPRequest data:")
  TextGadget(#PB_Any, 10, 200, 100, 20, "SOAP data:")
  TextGadget(#PB_Any, 10, 455, 100, 20, "Response data:")
  ListViewGadget(0, 10, 35, 480, 140)
  TreeGadget(1, 10, 230, 480, 200)
  TreeGadget(2, 10, 485, 480, 200)  
  
  soap.s = "<soapenv:Envelope xmlns:soapenv=" + 
           ~"\"http://schemas.xmlsoap.org/soap/envelope/\" " +  
           ~"xmlns:tem=\"http://tempuri.org/\">" +
           "<soapenv:Header/>" +
           "<soapenv:Body>" +
           "<tem:Add>" +
           "<tem:intA>5</tem:intA>" +
           "<tem:intB>3</tem:intB>" +
           "</tem:Add>" +
           "</soapenv:Body>" +
           "</soapenv:Envelope>"  
  
  NewMap Header.s()
  Header("Content-Type") = "text/xml;charset=UTF-8"
  Header("SOAPAction") = ~"\"http://tempuri.org/Add\""
  Header("Content-Length") = Str(StringByteLength(soap, #PB_UTF8))
  URL.s = "http://www.dneonline.com/calculator.asmx"
  request  = HTTPRequest(#PB_HTTP_Post, URL, soap, 0, Header())
  statusCode = Val(HTTPInfo(request, #PB_HTTP_StatusCode))
  
  AddGadgetItem(0, -1, "About to send the request")
  ForEach Header()
    AddGadgetItem(0, -1, MapKey(Header()) + ": " + Header())
  Next
  
  If ParseXML(0, soap)
    displayXML(0, 1) 
  EndIf
  
  If request And statusCode >= 200 And statusCode < 300

    response.s = HTTPInfo(request, #PB_HTTP_Response)
    FinishHTTP(request) 
    AddGadgetItem(0, -1, " ")
    AddGadgetItem(0, -1, "Status Code: Success!")   
    
    If ParseXML(0, response)
      displayXML(0, 2)       
    EndIf        
    
  Else
    
    AddGadgetItem(2, -1, "HTTP Response error: " + Str(statusCode))
    
  EndIf
  
  While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend 
  
Else
  
  MessageRequester("HTTPRequest Error:", "Unable to initialize network!")
  
EndIf
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Steving
User
User
Posts: 13
Joined: Tue Sep 12, 2017 11:40 pm
Location: San Francisco Bay Area
Contact:

Re: Trying to figure out why my HTTPRequest Post does not wo

Post by Steving »

Wow @infratec, I knew you were good but this one knocks it out of the park. I get a lot of good information reading your replies to other issues on this board. Thanks also TI-994A for your help with the output formatting. This helped me so much I can't believe you just got that so fast. All I did was paste your code in there and suddenly... it worked! You are so awesome I thank you so much.

Another problems bites the dust thanks to this forum!

Thanks again!

Steving
Post Reply