Run-time error with http setRequestHeader - rest

Public Function WebRequest(url As String, sVal1 As String, sVal2 As Integer) As String
Dim xmlhttp As MSXML2.xmlhttp
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.setRequestHeader "Content-Type", "application/json"
'xmlhttp.setRequestHeader "Accept", "text/xml"
'xmlhttp.setRequestHeader "Value1", sVal1
'xmlhttp.setRequestHeader "Value2", sVal2
xmlhttp.open "GET", url, False
xmlhttp.send
WebRequest = xmlhttp.responseText
Set xmlhttp = Nothing
End Function
When I run this function, even with the other paramaters commented out, it fails at the line: xmlhttp.setRequestHeader "Content-Type", "application/json" no matter what I put into the request header fields.
I get the error: Run-time error '-2147467259(80004005)' Unspecified error if I comment out the setRequestHeader line then it works fine, but the API returns that I am missing header fields (expected) I have ried MS XML v3.0 and v6.0 and am completely out of ideas...

Set your request headers after opening the request.

Related

Is postWith switching my request's Content-Type?

No matter what value I enter as my request's "Content-Type", the outgoing request I send out seems to replace it with "application/x-www-form-urlencoded". The application I'm trying to hit expects "application/json". My code, basically, is below.
{-# LANGUAGE OverloadedStrings #-}
import Network.Wreq
...
submissionResources = ["https://widgets.example.com/v2/widgets/request"]
sendWidgetToEndpoint submissionResources workingToken key widgetArray = do
let opts = defaults & header "Content-Type" .~ ["application/json"]
& header "apikey" .~ [key]
& header "Authorization" .~ [workingToken]
endPointURL = head submissionResources
widgetId = widgetArray !! 0
numberOfWidgets = widgetArray !! 1
widgetText = widgetArray !! 2
submissionResult <- postWith opts endPointURL [ "widgetId" := widgetId
, "numWidgets" := numberOfWidgets
, "widgetText" := widgetText
]
return submissionResult
My problem is that I keep getting back Status {statusCode = 415, statusMessage = "Unsupported Media Type"} from this endpoint, and I'm confident this is because the request I'm sending appears to be overriding "Content-Type" in my header. I have tried using "application/json" and "text/plain" but the response I get back always indicates to me that all the headers I sent over look as expected except for Content-Type which invariably has become "application/x-www-form-urlencoded".
How can I ensure wreq keeps 'Content-Type: application/json' in my requests header?
EDIT: I'm determining what headers were in my original request by what the API server tells me in its response back to me.
The type of the last argument to postWith in your snippet is [FormParam], and that type is what forces the Content-Type to be urlencoded.
To send JSON, send something of type Value or Encoding (from Data.Aeson).
import Data.Aeson (pairs, (.=))
...
-- also remove the "Content-Type" field from opts
submissionResult <- postWith opts endpointURL $ pairs
( "widgetId" .= widgetId <>
"numWidgets" .= numberOfWidgets <>
"widgetText" .= widgetText )
...
The Content-Type is set by the payload you pass to postWith, via the Postable instance. If you want to use yet another Content-Type header, define your own type with a Postable instance where you set an appropriate Content-Type. You can also choose to not set any Content-Type in the Postable instance, so you can set it via the options instead.

Azure media service job creation fails using rest api

Trying to consume Azure media service rest api. (following the tutorial : https://learn.microsoft.com/en-us/azure/media-services/media-services-rest-get-started)
Everything works fine until the point I try to create a Job. Sending the same request as in example (except asset id and token) and getting response :
Parsing request content failed due to: Make sure to only use property names that are defined by the type
Request:
POST https://wamsdubclus001rest-hs.cloudapp.net/api/Jobs HTTP/1.1
Connection: Keep-Alive
Content-Type: application/json
Accept: application/json; odata=verbose
Accept-Charset: UTF-8
Authorization: Bearer token -> here i send real token
DataServiceVersion: 1.0;NetFx
MaxDataServiceVersion: 3.0;NetFx
x-ms-version: 2.11
Content-Length: 458
Host: wamsdubclus001rest-hs.cloudapp.net
{
"Name":"TestJob",
"InputMediaAssets":[
{
"__metadata":{
"uri":"https://wamsdubclus001rest-hs.cloudapp.net/api/Assets('nb%3Acid%3AUUID%3A5168b52a-68ed-4df1-bac8-0648ce734ff6')"
}
}
],
"Tasks":[
{
"Configuration":"Adaptive Streaming",
"MediaProcessorId":"nb:mpid:UUID:ff4df607-d419-42f0-bc17-a481b1331e56",
"TaskBody":"<?xml version=\"1.0\" encoding=\"utf-8\"?><taskBody><inputAsset>JobInputAsset(0)</inputAsset> <outputAsset>JobOutputAsset(0)</outputAsset></taskBody>"
}
]
}
Response:
{
"error":{
"code":"",
"message":{
"lang":"en-US",
"value":"Parsing request content failed due to: Make sure to only use property names that are defined by the type"
}
}
}
It seems to be related with __metadata property. when I follow instruction from here : Creating Job from REST API returns a request property name error, the error changes:
"error":{
"code":"",
"message":{
"lang":"en-US",
"value":"Invalid input asset reference in TaskBody - "
}
}
}
Cant figure out whats wrong, thanks
Let me check on this, but it could be a couple issues that I have run into in the past.
First. Set both the Accept and Content-Type headers to:
"application/json; odata=verbose"
Next, double check that you are actually using the long underscore character on the metadata property. I've had issues where that was sending the wrong underscore character and it didn't match the property name.
Let me know if either of those helps.
It seems the issue was about "Content-Type". As I am using .net Core it was not easy to set the Conent-type as "application/json; odata=verbose".
1) Tried with RestSharp - dosnt support it, it cuts "odata=verbose" part out
2) Tried with Systsem.Net.Http.HttpClient -> Possible but difficult.
To add it as "Accept" :
MediaTypeWithQualityHeaderValue mtqhv;
MediaTypeWithQualityHeaderValue.TryParse("application/json;odata=verbose", out mtqhv);
client.DefaultRequestHeaders.Accept.Add(mtqhv);//ACCEPT header
To add it as "Content-Type" :
request.Content = new StringContent(content,
System.Text.Encoding.UTF8); //CONTENT-TYPE header -> default type will be text/html
request.Content.Headers.Clear(); // need to clear it - it will fail otherwise
request.Content.Headers.TryAddWithoutValidation("Content-Type","application/json;odata=verbose");

Check VAT Number at VIES with classic ASP + SOAP

I'm trying to check European VAT Number at VIES via Webservice:
with my poor knowledge I built the following routine:
<%
strEnvelope="<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:urn=""urn:ec.europa.eu:taxud:vies:services:checkVat:types"">"
strEnvelope=strEnvelope&"<soapenv:Header/>"
strEnvelope=strEnvelope&"<soapenv:Body>"
strEnvelope=strEnvelope&"<urn:checkVat>"
strEnvelope=strEnvelope&"<urn:countryCode>DE</urn:countryCode>"
strEnvelope=strEnvelope&"<urn:vatNumber>247856515</urn:vatNumber>"
strEnvelope=strEnvelope&"</urn:checkVat>"
strEnvelope=strEnvelope&"</soapenv:Body>"
strEnvelope=strEnvelope&"</soapenv:Envelope>"
set objHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
objHTTP.open "post", "http://ec.europa.eu/taxation_customs/vies/services/checkVatService"
objHTTP.setRequestHeader "Content-Type", "text/xml"
objHTTP.setRequestHeader "SOAPMethodName", "checkVat"
objHTTP.send strEnvelope
strReturn = objHTTP.responseBody
response.write strReturn
%>
but I got this answer
猼慯㩰湅敶潬数ç æ±­ç®çŒºæ…¯ãµ°æ ¢ç‘´ã©°â¼¯æ³æ•¨æ…­â¹³æµ¸ç¬æ…¯â¹°ç‰¯â½§æ½³ç¡æ”¯ç™®æ±¥ç¯â½¥ã¸¢çŒ¼æ…¯ã©°æ½‚祤㰾档æ¥å™«ç‘¡æ•’ç³æ¹¯æ•³ç æ±­ç®âˆ½ç‰µã©®æ¥æ”®ç‰µç¯â¹¡ç•¥çºç¡¡æ‘µç˜ºæ•©ã©³æ•³ç™²æ©ç¥æŒºæ•¨æ­£æ…–㩴祴数≳㰾潣湵牴ä¹æ‘¯ã¹¥ä•„⼼潣湵牴ä¹æ‘¯ã¹¥ç˜¼ç‘¡ç•Žæ‰­ç‰¥ãˆ¾ãœ´ã”¸ã”¶ã”±â¼¼æ…¶ä¹´æµµæ•¢ã¹²çˆ¼ç…¥æ•µç‘³æ…„整㈾㄰ⴷ㜰ㄭ⬵㈰〺㰰爯煥敵瑳慄整㰾慶楬㹤牴敵⼼慶楬㹤渼浡㹥ⴭ㰭港浡㹥愼摤敲ç³â´¾â´­â¼¼æ‘¡ç‰¤ç¥ã¹³â¼¼æ¡£æ¥å™«ç‘¡æ•’ç³æ¹¯æ•³ã°¾çŒ¯æ…¯ã©°æ½‚祤㰾猯慯㩰湅敶潬数> 猼慯㩰湅敶潬数ç æ±­çÂ®çŒºæ…¯ãµ°æ ¢ç‘´ã©°â¼¯æ³敨慭⹳浸ç¬慯⹰牯⽧潳ç¡æâ€Â¯Ã§â„¢Â®Ã¦Â±Â¥Ã§Â¯â½¥ã¸¢çŒ¼æ…¯ã©°æ½‚祤㰾档æ¥噫瑡敒ç³湯敳ç æ±­ç®∽牵㩮æÂ¥æâ€Â®Ã§â€°ÂµÃ§Â¯â¹¡ç•¥çº硡摵瘺敩㩳敳癲æ©ç¥挺敨正慖㩴祴数≳㰾潣湵牴ä¹摯㹥䕄⼼潣湵牴ä¹摯㹥瘼瑡畎扭牥㈾㜴ãâ€Â¸Ã£â€Â¶Ã£â€Â±Ã¢Â¼Â¼Ã¦â€¦Â¶Ã¤Â¹Â´Ã¦ÂµÂµÃ¦â€¢Â¢Ã£Â¹Â²Ã§Ë†Â¼Ã§â€¦Â¥Ã¦â€¢ÂµÃ§â€˜Â³Ã¦â€¦â€žÃ¦â€¢Â´Ã£Ë†Â¾Ã£â€žÂ°Ã¢Â´Â·Ã£Å“°ã„­â¬µãˆ°ã€ºã°°çˆ¯ç…¥æ•µç‘³æ…„整㰾慶楬㹤牴敵⼼慶楬㹤渼浡㹥ⴭ㰭港浡㹥愼摤敲ç³ⴾⴭ⼼摡牤ç¥㹳⼼档æ¥噫瑡敒ç³湯敳㰾猯慯㩰潂祤㰾猯慯㩰湅敶潬数>
can give some hints?
Thanks
The text representation of the response is in the responseText property, so:
strReturn = objHTTP.responseText

400 Bad Request : Consuming WCF basicHttpBinding (Soap) using JScript/VBScript

var oXMLDoc, oXMLHttp, soapRequest, soapResponse;
oXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
oXMLHttp.open("POST", "http://nerdbox/HelloService.svc", false);
// Add HTTP headers
oXMLHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
oXMLHttp.setRequestHeader("SOAPAction", "http://tempuri.org/IHelloService/SayHello");
// Form the message
soapRequest = '<?xml version="1.0" encoding="utf-16"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><SayHello xmlns="http://tempuri.org/"><name>Zuhaib</name></SayHello></soap:Body></soap:Envelope>';
WScript.Echo("Request : " + soapRequest);
oXMLHttp.send(soapRequest);
soapResponse = oXMLHttp.responseXML.xml;
WScript.Echo("Respose : " + soapResponse);
Whats wrong with this JScript? why am I getting 400 Bad Request. I read similar threads in stackoverflow .. some say its soap message formatting problem.
This is what the message looks like if I take it from fiddler.
Try changing your action from IHelloService to HelloService.
And let me ask you, why are you doing it the hard way. Just add a webHttpBinding and use JSON.
See a very easy example here.
I had to change your code to the following to get it to run in VBSEdit...then I (obviously) got the error about it not being able to find the resource...but change your code to this and see if it makes a difference?
Dim oXMLDoc, oXMLHttp, soapRequest, soapResponse
Set oXMLHttp = CreateObject("Microsoft.XMLHTTP")
oXMLHttp.open "POST", "http://nerdbox/HelloService.svc", False
'// Add HTTP headers
oXMLHttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
oXMLHttp.setRequestHeader "SOAPAction", "http://tempuri.org/IHelloService/SayHello"
'// Form the message
soapRequest = "<?xml version=""1.0"" encoding=""utf-16""?><soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><soap:Body><SayHello xmlns=""http://tempuri.org/""><name>Zuhaib</name></SayHello></soap:Body></soap:Envelope>"
WScript.Echo "Request : " + soapRequest
oXMLHttp.send soapRequest
soapResponse = oXMLHttp.responseXML.xml
WScript.Echo "Respose : " + soapResponse

Sending a 'application/soap+xml' SOAP request using Classic ASP

Any help with this would be appreciated; I've been at it for a few days now.
Below is the code that I've got so far; unfortunatly when I run it I get a HTTP 415 error; Cannot process the message because the content type 'text/xml; charset=UTF-8' was not the expected type 'application/soap+xml; charset=utf-8'.
I have to send the content-type of application/soap+xml as this is the only type that the web service allows; and I have to do it in classic ASP.
I've tried changing the 'send' line to "objRequest.send objXMLDoc.XML" but this then gives me a HTTP 400 Bad Request error.
strXmlToSend = "<some valid xml>"
webserviceurl = "http://webservice.com"
webserviceSOAPActionNameSpace = "avalidnamespace"
Set objRequest = Server.createobject("MSXML2.XMLHTTP.3.0")
objRequest.open "POST", webserviceurl, False
objRequest.setRequestHeader "Content-Type", "application/soap+xml"
objRequest.setRequestHeader "CharSet", "utf-8"
objRequest.setRequestHeader "action", webserviceSOAPActionNameSpace & "GetEstimate"
objRequest.setRequestHeader "SOAPAction", webserviceSOAPActionNameSpace & "GetEstimate"
Set objXMLDoc = Server.createobject("MSXML2.DOMDocument.3.0")
objXMLDoc.loadXml strXmlToSend
objRequest.send objXMLDoc
set objXMLDoc = nothing
Here's what I've used successfully in the past:
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xmlhttp.open "POST", url, false
xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
xmlhttp.setRequestHeader "SOAPAction", "http://www.mydomain.com/myaction"
xmlhttp.send postdata
xml = xmlhttp.responseText
When you pass an XML DOM ot the send method the Content-Type is always set to "text/xml".
If you want to control the content type then you must pass a string. Don't bother loading the XML string into a DOM only to call the xml property since that may change the content of the xml declaration. BTW what does the xml declaration look like in the XML string and are you certain that the xml is correct? The encoding on the xml declare if present should say "UTF-8".
Don't send a header CharSet it means nothing, CharSet is an attribute of the Content-Type header.
Don't use XMLHTTP from inside ASP its not safe.
Hence your code ought to look like this:-
strXmlToSend = "<some valid xml>"
webserviceurl = "http://webservice.com"
webserviceSOAPActionNameSpace = "avalidnamespace"
Set objRequest = Server.Createobject("MSXML2.ServerXMLHTTP.3.0")
objRequest.open "POST", webserviceurl, False
objRequest.setRequestHeader "Content-Type", "application/soap+xml; charset=UTF-8"
objRequest.setRequestHeader "action", webserviceSOAPActionNameSpace & "GetEstimate"
objRequest.setRequestHeader "SOAPAction", webserviceSOAPActionNameSpace & "GetEstimate"
objRequest.send strXmlToSend
Not sure about that "action" header either looks superflous to me. Perhaps this will still fail in some way but it shouldn't complain about the Content-Type header anymore.