Rest Api Post file (curl convert) February 23, 2022 09:03AM |
Registered: 2 years ago Posts: 2 |
curl --location --request POST '[mysite]; \ --header 'Accept: application/vnd.site+json' \ --header 'Content-Type: multipart/form-data' \ --header 'version: 3.0' \ --header 'Authorization: Bearer apikey' \ --form 'invoice_file=@"/C:/211031-3775379.pdf"'
sAutl_url is UNICODE string MyBuffer is Buffer MyBuffer = fLoadBuffer("C:/211031-3775379.pdf") sAutl_url="[mysite]; cMyRequest is restRequest cMyRequest..URL = sAutl_url cMyRequest..Header["Accept"] = "application/vnd.site+json" cMyRequest..Header["version"] = "3.0" cMyRequest..ContentType = "multipart/form-data" cMyRequest..Header["Authorization"] = "Bearer myapikey" //cMyRequest..Content =["invoice_file" , MyBuffer ] //false //cMyRequest..Content =[{"invoice_file" , MyBuffer }] //false //cMyRequest..Content =[{"invoice_file" , MyBuffer }] //false //cMyRequest..Content ="invoice_file " + //{"status":400,"error":"Bad Request"} //cMyRequest..Content ="invoice_file:" +MyBuffer //{"status":400,"error":"Bad Request"} //cMyRequest..Content ="invoice_file," + MyBuffer //{"status":400,"error":"Bad Request"} //cMyRequest..Content = "invoice_file " // {"errors":[{"code":"invoice_file","messages":["Missing invoice file"]}]} //cMyRequest..Content =MyBuffer //{"status":400,"error":"Bad Request"} vContent is Variant //vContent."form-data"[1]="invoice_file" //vContent."form-data"[2]=sfile //vContent."form-data"[2]=MyBuffer //{"errors":[{"code":"invoice_file","messages":["Missing invoice file"]}]} vContent.invoice_file =MyBuffer //{"errors":[{"code":"invoice_file","messages":["Missing invoice file"]}]} cMyRequest..Content = vContent cMyRequest..Method = httpPost cMyResponse is restResponse = RESTSend(cMyRequest) IF InTestMode= True OR testmode_value() = True THEN Trace(cMyResponse.StatusCode) Trace(AnsiToUnicode( cMyResponse..Content)) END IF ErrorOccurred THEN Error(ErrorInfo(errFullDetails)) sResult_tmp = ErrorInfo(errFullDetails) ELSE IF cMyResponse.StatusCode = 200 OR cMyResponse.StatusCode = 201 THEN sResult_tmp = AnsiToUnicode( cMyResponse..Content) IF InTestMode= True OR testmode_value() = True THEN Trace(sResult_tmp) END END END
Re: Rest Api Post file (curl convert) February 25, 2022 05:21PM |
Registered: 5 years ago Posts: 41 |
Re: Rest Api Post file (curl convert) March 17, 2022 11:23AM |
Registered: 2 years ago Posts: 2 |
Re: Rest Api Post file (curl convert) March 19, 2022 05:22PM |
Registered: 3 years ago Posts: 107 |
FUNCTION JDEMOFileUpload(pErrLst is soErrorList, pMOStructure is string, pMOKey is string, pMOSequence is int, pItemName is string, pFileName is string, pServiceURL is string=AISVersionMediaObjectService+["/"]+"file/upload", ...) pToken is string = :AISToken, pUserName is string = :JDEUserName, pUserPassword is string = :JDEUserPassword)boolean,JSON) //POST Doc: [docs.oracle.com] //Local variables Success is boolean = False ErrMsg is soErrorMessage RESTRqst is restRequest RESTResp is restResponse JSONRqst is JSON <description="v2.file.upload"> JSONResp is JSON <description="resp.v2.file.upload"> JSONError is JSON <description="resp.vx.orchestrator.service-error"> MimeType is string FormName is string = GetGUID(guidRough) JSONFile is string = TMPFileDir+[fSep]+FormName+".json" WHEN EXCEPTION IN //Set Mime Type SWITCH Lower(fExtractPath(pFileName,fExtension)) CASE ~= ".jpg",".jpeg",".png",".gif",".webp",".tif",".tiff",".psd",".raw",".bmp",".heif",".indd",".ai",".eps": MimeType = "image/"+NoCharacter(Lower(fExtractPath(pFileName,fExtension)),".") CASE ~= ".svg": MimeType = "image/svg+xml" CASE ~= ".mpeg",".ogv",".webm": MimeType = "video/"+NoCharacter(Lower(fExtractPath(pFileName,fExtension)),".") CASE ~= ".avi": MimeType = "video/x-msvideo" CASE ~= ".aac",".mp3",".wav": MimeType = "audio/webm" CASE ~= ".weba": MimeType = "image/svg+xml" CASE ~= ".pdf": MimeType = "application/pdf" CASE ~= ".txt",".text",".log": MimeType = "text/plain" CASE ~= ".rtf": MimeType = "application/rtf" CASE ~= ".doc",".dot": MimeType = "application/msword" CASE ~= ".docx",".docm",".dotx",".dotm",".docb": MimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" CASE ~= ".xls",".xlt": MimeType = "application/vnd.ms-excel" CASE ~= ".xlm",".xlsx",".xlsm",".xltx",".xltm": MimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" CASE ~= ".ppt",".pot",".pps": MimeType = "application/vnd.ms-powerpoint" CASE ~= ".pptx",".pptm",".potx",".potm",".ppsx",".ppsm",".sldx",".sldm": MimeType = "application/vnd.openxmlformats-officedocument.presentationml.presentation" OTHER CASE : MimeType = "application/"+NoCharacter(Lower(fExtractPath(pFileName,fExtension)),".") END //Check if token is still valid JDETokenSessionExists(pErrLst,pToken) //Build JSON Request JSONRqst.token = pToken //Don't set device name as it will break the session // <IF ExecutionTarget=Android> // JSONRqst.deviceName = NetIPAddress() // <ELSE> // JSONRqst.deviceName = NetMachineName() // <END> JSONRqst.moStructure = pMOStructure JSONRqst.moKey = pMOKey JSONRqst.formName = "" JSONRqst.version = "" JSONRqst.file.fileName = fExtractPath(pFileName,fFileName+fExtension) JSONRqst.file.fileLocation = pFileName JSONRqst.file.itemName = pItemName JSONRqst.file.sequence = pMOSequence //For upload we need to send the JSONRequest as a JSON file <COMPILE IF ConfigurationType = Android _OR_ ConfigurationType = iOS> fHandle is int = fOpen(JSONFile,foCreate) <ELSE> fHandle is int = fOpen(JSONFile,foUnicode+foCreate) <END> IF fHandle > 0 THEN Success = True SerBuf is Buffer Serialize(JSONRqst, SerBuf, psdJSON) fWrite(fHandle,SerBuf) fClose(fHandle) ELSE Success = False ErrMsg:AddToList(pErrLst,soErrorMessage.EM_ERROR,"FILE IO",StringBuild("An error was encountered while trying to write the .json request file '%1'.",TMPFileDir+[fSep]+JSONFile+".json"),ErrorInfo(errCode)+": "+ErrorInfo(errMessage)) END //If the json was created IF Success THEN //Create the form HTTPCreateForm(FormName) HTTPAddFile(FormName,"file",pFileName,MimeType) HTTPAddFile(FormName,"moAdd",JSONFile,typeMimeJSON) //Set REST Request Parameters and fill form fields RESTRqst..Header["jde-AIS-Auth"]=pToken RESTRqst..UserAgent = "Pixontri JDEdwards REST" //The content is sent as form-data RESTRqst..ContentType = typeMimeMultiPartForm //-->multipart/form-data //RESTRqst..Content = "" //-->no content -> form RESTRqst..Method = httpPost RESTRqst..URL = :JDEEndPointURL+["/"]+pServiceURL RESTRqst..User = pUserName RESTRqst..Password = pUserPassword //Execute REST request RESTResp = HTTPSendForm(FormName,RESTRqst) //RestSend(RESTRqst) //Server was reachable? IF NOT ErrorOccurred THEN //Evaluate REST response //Possible values for this request: // 200 Response Successful Execution // 400 Response Bad Request - Invalid JSON Input // 403 Response Authorization Failure // 405 Response Allowed Hosts on the HTML server is not configured to accept requests from this AIS Server // 415 Response Invalid Content-Type Header // 445 Response Mobile SSO Failed // 446 Response Mobile SSO Requested, but SSO is not Enabled on this AIS Server // 500 Response Server Failed to Process Request SWITCH RESTResp.StatusCode CASE 200: JSONResp = RESTResp.Content IF NOT JSONResp.uniquefilename ~= "" AND Val(JSONResp.sequence) > 0 THEN Success = True ELSE Success = False END CASE 444,500: JSONError = RESTResp.Content ErrMsg:AddToList(pErrLst,soErrorMessage.EM_ERROR,"HTTP REST",JSONError.message..Value+" ["+JSONError.type..Value+"]","Exception: "+JSONError.exception..Value+CR+JSONError.userDefinedErrorText..Value) Success = False OTHER CASE: ErrMsg:AddToList(pErrLst,soErrorMessage.EM_ERROR,"HTTP REST","An error was encountered while trying to login to JDE.",RESTResp.StatusCode+": "+RESTResp.DescriptionStatusCode) Success = False END ELSE ErrMsg:AddToList(pErrLst,soErrorMessage.EM_ERROR,"HTTP REST","An error was encountered while trying to login to JDE.",ErrorInfo(errCode)+": "+ErrorInfo(errMessage)) Success = False END END //Clear the temporary upload and JSON file fDelete(JSONFile,frReadOnly) fDelete(pFileName,frReadOnly) //Return Result RESULT (Success, JSONResp) DO WXApplication::LogException(pErrLst) <COMPILE IF ConfigurationType<>Android> ExceptionEnable() <END> RESULT (False, Null) END