Chris86

REST API - Post XML data with HTTP

2 posts in this topic

#1 ·  Posted (edited)

Hello,

I'm in the making of a script that needs to post XML data through HTTPS to our ServiceDesk Plus MSP service. I'm running into trouble because I have never made any scripts using HTTP with POST and I cannot see what I'm doing wrong here.

Can someone help me or should I find help somewhere else?

$URLSDP="" ;REMOVED - cannot publish this. this is the HTTPS url to our ServiceDesk
$TechnicianKey="" ;REMOVED
$ID="" ; Request ID

$sPostData = "OPERATION_NAME=ADD_NOTE&INPUT_DATA=<Operation><Details><Notes><Note><isPublic>false</isPublic><notesText>Text added to the note</notesText></Note></Notes></Details></Operation>&format=XML"
$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("POST", $URLSDP & "/sdpapi/request/"& $ID & "/note?TECHNICIAN_KEY="& $TechnicianKey, False)
$oHTTP.SetRequestHeader("Content-Type", "xml")
$oHTTP.Send($sPostData)
$oReceived = $oHTTP.ResponseText
$oStatusCode = $oHTTP.Status

ConsoleWrite($oStatusCode & @CRLF)
ConsoleWrite($oReceived & @CRLF)
If $oStatusCode = 200 Then
    ;Process the response $oReceived
    ;ConsoleWrite(@CRLF & "Response" & @CRLF & $oReceived & @CRLF)
Else
    MsgBox(16, "Error " & $oStatusCode, $oReceived, 7)
EndIf

Response:

<operation name="POST"><result><status>Failed</status><message>Invalid operation</message></result></operation>

REST API guide.

https://www.manageengine.com/products/service-desk-msp/help/adminguide/api/notes-operations.html#add

 

Edited by Chris86

Share this post


Link to post
Share on other sites



Search for some HttpDebugger to see how exactly header look like.

I suggest you to do something like this:

$objHTTP.open ("POST", "url", False)
$objHTTP.setRequestHeader ("Content-Type", "text/xml;charset=UTF-8")
$objHTTP.setRequestHeader("Content-Length",StringLen($YourXMLText))
$objHTTP.setRequestHeader("Connection","If is needed")
$objHTTP.setRequestHeader("User-Agent","If is needed")
$objHTTP.send ($YourXMLText)

Saludos

Share this post


Link to post
Share on other sites

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

  • Similar Content

    • Jefrey
      By Jefrey
      I've ported these two functions from PHP to AU3 to work with URLs.
      Made them for those who work with libraries like HTTP.au3 (not the one I coded), that needs passing the server domain, path, etc., instead of the full URL.
      Grab the lib here.
      ParseURL( $sURL )
      Parses the URL and splits it into defined parts. Returns an array:
      [0] = Full URL (same as $sURL) [1] = Protocol (i.e.: http, https, ftp, ws...) [2] = Domain [3] = Port (or null if not specified) [4] = Path (or null if not specified) [5] = Query string (everything after the ? - or null if not specified) Example:
      $aExample = ParseURL("https://google.com:8080/?name=doe") MsgBox(0, "Test", "URL: " & $aExample[0] & @CRLF & _ "Protocol: " & $aExample[1] & @CRLF & _ "Domain: " & $aExample[2] & @CRLF & _ "Port: " & $aExample[3] & @CRLF & _ "Path: " & $aExample[4] & @CRLF & _ "Query string: " & $aExample[5])  
      ParseStr( $sStr )
      Parses a query string (similar to the [5] of the previous function) and returns a multidimensional array, where:
      [0][0] = number of variables found [0][1] = ununsed [1][0] = key name of the first variable [1][1] = first variable value (already URL decoded) [n][0] = key name of the nth variable [n][1] = nth variable value (already URL decoded) Example:
      include <Array.au3> ; need only to do _ArrayDisplay, not needed by the lib _ArrayDisplay(ParseStr("foo=bar&test=lol%20123")) #cs Result is: [0][0] = 2 [0][1] = ununsed [1][0] = foo [1][1] = bar [2][0] = test [2][1] = lol 123 #ce Feel free to fork!
    • mjolnirmarkiv
      By mjolnirmarkiv
      Hi!
      I have a potential problem of memory leakage in the script I wrote and wonder if anyone will be able to suggest a potential source of the problem?
      The script is desgined to log into secure website and constantly send HTTP GET request to obtain a list of tasks the client has assigned to us (the tasks are auctions, so swift actions are neccessary, hence the script) and then occasionally send HTTP POST to accept certain tasks based on certain criteria. It works 24/7 and works alright, but I noticed that amount of RAM it uses will increase significantly over time (say, it jumps from 20 000 K to 100 000 K in a few days).
      I cannot post the entire script: it modestly big, also might contatin some sensetive information. But here's the main loop (like, exactly):
      While True If Script_SignIn() And Script_GetTasks() Then Script_AcceptTasks() Sleep(10) WEnd The Script_ functions are here just to simplify the structure, they don't pass any parameters to each other, just return True \ False when finished and change some global variables when executed. Entire GUI is in OnEvent mode.
      TMS_ functions are where the actual work is done, names are self-explanatory:
      TMS_SignIn($sURL, $sUsername, $sPassword) -- returns $sAuthKeys (used subsequently in other functions to authenticate yourself, while session is active). TMS_GetTasks(ByRef $oHTTP, $sURL, $sAuthKeys) -- this function is running constantly returning $avTasks (basically a table with all information I need to accept them), I'm passing a global variable $g_oHTTP to it, it creates HTTP object only once per session (hence ByRef, I don't want to locally create HTTP object at every pass, just create once and reuse) and in next loop I pass the object the function created to it again, $g_oHTTP is destroyed when session has ended and recreated in next loop after re-login. TMS_AcceptTasks(ByRef $avTasks, $sURL, $sAuthKeys) -- returns True\False (changes $avTasks table slightly -- the status of acceptance if it's successful). TMS_SignOut($sURL, $sAuthKeys) -- returns True\False. These functions create local HTTP and HTML objects inside as well, I even "= 0" them at the end, even though I don't think I need to do this, since they are local variables. Still the problem persists.
      Any ideas where to look?
    • mLipok
      By mLipok
      I was asking @eltorro serveral times for any support  for XML DOM wrapper (COM) - with no success  
          So I took matters into my hands ..... I want to present XMLWrapperEx.au3 - BETA Version
      Want to join to the project ?
       
      Here is some description:
      ; #INDEX# ======================================================================================================================= ; Title .........: XMLWrapperEx.au3 ; AutoIt Version : 3.3.10.2++ ; Language ......: English ; Description ...: Functions to use for reading and writing XML using msxml. ; Remarks .......: BETA Version ; Author ........: mLipok ; Version .......: "1.1.1.01" ; _XML_MiscProperty_UDFVersion() #CS This UDF is created on the basis of: https://www.autoitscript.com/forum/topic/19848-xml-dom-wrapper-com/ For this reason, I attach also the last known (to me) previous version ($_XMLUDFVER = "1.0.3.98" _XMLDomWrapper_1.0.3.98_CN.au3 ) For the same reason I continue to recognize the achievements of the work of my predecessors (they are still noted in each Function header). . . . . !!!!!!!!! This is BETA VERSION (all could be changed) !!!!!!!!! . . . WORK IN PROGRES INFORMATION: For now 2015-09-01 the descripion (Function Header) can not entirely correctly describe the function. TODO: in many places I used "TODO" as a keyword to find what should be done in future . I want to: PREVENT THIS: The unfortunate nature of both the scripts is that the func return results are strings or arrays instead of objects. .     I want to: USE THIS CONCEPT:     .   All function should use Refernce to the object as first Function parameter     .   All function should return in most cases objects. There should be separate functions to Change Object collection to array     .   All function should use COM Error Handler in local scope.     .   All function should return @error which are defined in #Region XMLWrapperEx.au3 - @ERROR Enums     .    All function should have the same naming convention     .    All variables should have the same naming convention     .    There should not to be any Global Variable - exception is $g__oXMLDOM_Events     .   It should be possible easy to use XML DOM Events     .        https://msdn.microsoft.com/en-us/library/ms764697(v=vs.85).aspx     .   It should be possible easy to Debug     .    Ultimately, you should be able to do anything with your XML without having to use your own Error Handler. #CE  
      More info inside zip archive.
      This UDF can be downloaded from here:
      REMARK:
      This UDF was formerly named:   XMLWrapperEx.au3 
       
    • 31290
      By 31290
      Hi Guys, 
      Since I'm able to get a Dell equipment warranty status thanks to my API key, I'm using an UDF to extract data from an XML file and get the end date. > 
      Thing is, when using InetGet, the original file is in JSON format and the UDF is not working anymore, even if I download the file with the xml extension. Therefore, and when I manually download the page with Chrome, I have a proper XML file where the UDF is working fine.
      Here's my code:
      I even tried to convert the json to xml > https://www.autoitscript.com/forum/topic/185717-js-json-to-xml/
      I took a look here https://www.autoitscript.com/forum/topic/104150-json-udf-library-fully-rfc4627-compliant/ but I don't understand anything :/
       
      The XML read UDF is just perfect for my needs but I'm stuck here... 
      Thanks for any help you can provide
      -31290-
      3MTXM12.json
      3MTXM12.xml
    • Chiitus
      By Chiitus
      Hello guys!
      Im wanting to send a HTTP PATCH method to GitHubAPI without authentication, but i just found how to send GET and POST methods.
      Any help please,
      THanks adv!
       
      EDIT: Problem solved!
      Using curl cmd now, thanks!