Question
· May 27, 2022

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 RootMIMEPart=##class(%Net.MIMEPart).%New()      
     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()
     Do Httprequest.SetHeader("Authorization","Bearer "_Token)
  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}
    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

Product version: Ensemble 2014.1
Discussion (4)1
Log in or sign up to continue

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