HTTP/1.1 415 Unsupported Media Type error when posting httprequest
Hi Guys,
I've a JSON file and I'm using the below code to post it but I'm geting "HTTP/1.1 415 Unsupported Media Type" error, could help pls?
Set BinaryMIMEPart=##class(%Net.MIMEPart).%New()
Set contentdisp="form-data; name="_$CHAR(34)_"file"_$CHAR(34)_"; filename="_$CHAR(34)_""_$CHAR(34)
Do BinaryMIMEPart.SetHeader("Content-Disposition",contentdisp)
Set stream=##class(%FileBinaryStream).%New()
Set stream.Filename=FilePath
Do stream.LinkToFile(FilePath) Set BinaryMIMEPart.Body=stream
Do BinaryMIMEPart.SetHeader("Content-Type","text/plain")
Set TextMIMEPart=##class(%Net.MIMEPart).%New()
Set TextMIMEPart.Body=##class(%GlobalCharacterStream).%New()
Do TextMIMEPart.Body.Write(FilePath)
Set TextMIMEPart.ContentType="text/plain"
Set TextMIMEPart.ContentCharset="us-ascii"
Do RootMIMEPart.Parts.Insert(BinaryMIMEPart)
Set writer=##class(%Net.MIMEWriter).%New()
Set SentHttpRequest=##class(%Net.HttpRequest).%New()
Set status=writer.OutputToStream(SentHttpRequest.EntityBody)
if $$$ISERR(status) {do $SYSTEM.Status.DisplayError(status) Quit}
Set status=writer.WriteMIMEBody(RootMIMEPart)
if $$$ISERR(status) {do $SYSTEM.Status.DisplayError(status) Quit}
S IP=$P(CallbackHost,"//",2)
Set SentHttpRequest.Server=$P(IP,"/")
et SentHttpRequest.Https=1
Set SentHttpRequest.SSLConfiguration="RTLS"
Set ContentType= "multipart/form-data; boundary="_RootMIMEPart.Boundary
Set SentHttpRequest.ContentType=ContentType
set url="/"_$P(IP,"/",2,*)
set status=SentHttpRequest.Post(url)
Set StateCode=SentHttpRequest.HttpResponse.StatusCode
Set StateL=SentHttpRequest.HttpResponse.StatusLine
Set Resp=SentHttpRequest.HttpResponse.Data.Read()
Thanks
How certain are you that the server you're sending the request to accepts a content type of "text/plain"? Having that wrong, or using the wrong encoding are probably the most common causes of a 415 error.
Not 100% sure but I think it should be a JSON format so how can I change my code for JSON?
sorry I'm a newbie, what do you mean encoding are referring to "text/plain" or "application/JSON" for example?
Thanks
If you're encoding your data before sending it, you have to specify how it was encoded in a content encoding header so that the server you're sending data to knows how to decode it.
I think it's more likely, though, that it's an issue with your content type. Where you're setting it to "text/plain", if it's supposed to be json, you might try setting it to "application/json" instead.
I already did but with no luck.
But not sure why previous colleague hwo let the company is using %Net.MIMEPart, I haven't used this before I thought I need to send the JSON file is something like this:
Set stream=##class(%FileBinaryStream).%New()
Set stream.Filename=FilePath
Do stream.LinkToFile(FilePath)
if (CallbackHost ["VIBRA-API-DEV.AZUREWEBSITES.NET") {
Set Httprequest=##class(%Net.HttpRequest).%New()
Set Httprequest.SSLConfiguration="RTLS"
Set Httprequest.Server="vibra-api-dev.azurewebsites.net"
Set Httprequest.Timeout=30
Set Httprequest.Https=1
set Httprequest.ContentType="application/json"
Do Httprequest.SetHeader("Accept","application/json")
Do Httprequest.SetHeader("Accept-Language","en_US")
Do Httprequest.SetHeader("Authorization","Bearer "_^Token)
D Httprequest.EntityBody.Write(stream)
Set tSc=Httprequest.Post("/API/SENSORS/VIBRATION")
//S Out=Httprequest.HttpResponse.Data.ReadLine()
Set StateCode=Httprequest.HttpResponse.StatusCode
Quit StateCode
would this work?
thanks