0

I'm using Microfocus UFT/QTP to automate a WPF app. I'm new to UFT and VBS. I need to call a REST API (GET) and parse the json response. It's a simple API with basic authentication and one parameter in the request but i can't get this to work. Please look into the code below and let me know what i'm doing wrong. I'm specifically getting "Error 415 Unsupported media type". Thanks!

Rough code below:

userName    =   "xyz@abc.com"
password    =   "Test123"
strJSONToSend = "{""accountNumber"": ""0132104201""}"
URL="https://blah-xyzucc.integration.ocp.blah.com:999/ic/api/integration/v1/flows/rest/blah_CUSTX_INTERN_blah_ACCNTSEARCH/1.0/accountSearch" 
Set objXmlHttpMain = CreateObject("Msxml2.ServerXMLHTTP") 
objXmlHttpMain.open "GET",URL, False, userName, password
objXmlHttpMain.setRequestHeader "charset","UTF-8"
objXmlHttpMain.setRequestHeader "Accept", "application/json"
objXmlHttpMain.send(strJSONToSend)
objXmlHttpMain.responseText
Ravi
  • 29
  • 1
  • 5
  • Are you sure the service at the other end is able to return json? – Hursey Jan 28 '20 at 20:39
  • Yes, Because when i call the same API using SOAPUI, i get the json response. – Ravi Jan 28 '20 at 20:50
  • Ok, so re-read the question. Should this be tagged vb.net as it looks like the code you've posted is vbscript? My vbscripting is quite rusty so will have to leave it to someone else – Hursey Jan 28 '20 at 20:55
  • I added the tag as most of the vb developers understand both the flavors – Ravi Jan 28 '20 at 21:02
  • Don’t include VB.Net in a VBScript question. They are completely different technologies based on the same foundation of the VB programming language but their differences are vast. As such questions tagged with both will lead to confusion and ultimately frustration when you can’t recreate a VB.Net solution in VBScript. – user692942 Jan 28 '20 at 21:48
  • 2
    Does this answer your question? [How to POST JSON Data via HTTP API using VBScript?](https://stackoverflow.com/questions/26956256/how-to-post-json-data-via-http-api-using-vbscript) *(This answer shows how to correctly structure the JSON string)* – user692942 Jan 28 '20 at 21:53
  • It didn't solve my problem.. That's exactly how i have built my code.. I get "Error 415 Unsupported media type". When i do this 'objXmlHttpMain.open "GET",URL, False , userName, password' then a null response.. – Ravi Jan 28 '20 at 22:10
  • But the `strJSONToSend` variable in the code example is not a valid VBScript string variable? Before you even get a response I'd expect VBScript to be returning a Syntax Error. If you expect help, you need to provide a working example to avoid being frustrated by the suggestions you will continue to receive. – user692942 Jan 28 '20 at 22:32
  • 1
    If you want to send JSON data you need to use a `POST` not `GET` to send the JSON body which is why you are getting HTTP 415 Unsupported media type. You can't say the request Content-Type is `application/json` if you aren't going to "post" the JSON body. If you expect to use `GET` then the Content-Type you are setting is wrong. Doing that though would be really strange approach when using POST is far more straightforward. The `GET` method should *(especially in RESTful architectures)* be used to retrieve resources not create/update them. – user692942 Jan 28 '20 at 23:24
  • @Lankymart : Thanks for that suggestion! I think that took me close to the solution. With "POST" I was getting "Error 405 -- Method Not allowed". This API call is to just retrieve the data so was using "GET". Sorry for not making it clear. I removed the 'Content-type' and with "GET" i'm now getting the below response: (code in the question updated) "{ "searchResponse" : { "status" : "FAILURE", "errorDescription" : "Property [value] is non-nullable", "errorCode" : "{http://schemas.oracle.com/bpel/extension}remoteFault" } }" – Ravi Jan 29 '20 at 14:39
  • Does this answer your question? [HTTP GET in VBS](//stackoverflow.com/q/204759) *(If you are using GET then you don't send anything in the body, but use the `?` querystring instead to query what is returned. Check the API documentation for further help)* – user692942 Jan 29 '20 at 14:44

0 Answers0