Help with Rest API call (POST) with parameters and CSV attachment
Hello Team,
please can someone help me in the below.
I'm trying to call a Rest API below specification working in postman, and receiving perfectly the response:
POST /PharmacyServices/api/Pharmacy/Upload?Key=aaaa&Username=bbb&Password=ccc HTTP/1.1
Host: abc:38440
Content-Length: 240
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="File"; filename="///xxxx/POC/CSV/20230607.csv"
Content-Type: text/csv
(data)
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Here's what i'm trying to do in IRIS:
Set docpath = "\\xxxx\MediTrack\" _ pRequest.FileName _ ".csv"
try{
Set HttpRequest = ##class(%Net.HttpRequest).%New()
set HttpRequest.Server=..Adapter.HTTPServer
set HttpRequest.Port=..Adapter.HTTPPort
//set HttpRequest.ProxyServer=..Adapter.ProxyServer
//set HttpRequest.ProxyPort=..Adapter.ProxyPort
set HttpRequest.ProxyTunnel=..Adapter.ProxyHttpTunnel
set HttpRequest.ProxyHTTPS=1
set HttpRequest.SSLConfiguration=..Adapter.SSLConfig
Do HttpRequest.SetHeader("Accept", "'*/*'")
Do HttpRequest.SetHeader("Access-Control-Allow-Origin","*")
set stream=##class(%Stream.FileCharacter).%New()
set sc=stream.LinkToFile(docpath)
Set RootMIMEPart = ##class(%Net.MIMEPart).%New()
Set BinaryMIMEPart = ##class(%Net.MIMEPart).%New()
Set BinaryMIMEPart.Body = stream
Do BinaryMIMEPart.SetHeader("Content-Type", "text/csv")
Do BinaryMIMEPart.SetHeader("Content-Disposition","form-data; name=""File""; filename="""_docpath_"""")
S status = RootMIMEPart.Parts.Insert(BinaryMIMEPart)
Set writer = ##class(%Net.MIMEWriter).%New()
Do writer.OutputToStream(HttpRequest.EntityBody)
Do writer.WriteMIMEBody(RootMIMEPart)
Do HttpRequest.SetHeader("Content-Type", "multipart/form-data; boundary="_RootMIMEPart.Boundary)
Do HttpRequest.SetHeader("Content-Disposition","form-data; name=""File""; filename="""_docpath_"""")
Do HttpRequest.SetParam("File", BinaryMIMEPart)
set tSCx = HttpRequest.Post(tURL, 0, 1) If '$IsObject( HttpRequest.HttpResponse) {
s err = "Response Error"
$$$TRACE(err)
}
Set Myresponse = HttpRequest.HttpResponse
If $IsObject(Myresponse) {
set data1 = ""
set stream1 = Myresponse.Data
While 'stream1.AtEnd {
set data1 = data1 _ stream1.ReadLine(,.sc,.eol)
If $$$ISERR(sc) { set data1 = data1 _ "ERROR" Quit }
}
$$$TRACE(data1)
}
But i'm receiving an error from the API stating that:
Invalid 'HttpContent' instance provided. It does not have a content-type header value. 'HttpContent' instances must have a content-type header starting with 'multipart/'.Parameter name: content"
Any idea what should be changed?
Comments
It was solved by WRC (InterSystems Support).
Anyway, the code above should work fine as tested by WRC. You can use HttpRequest.Post(tURL, 1, 1) to just display the request on the current device and compare it with the request sent by Postman, if there are any differences.