how to send HTML multipart form

guiyote
Member Posts: 12
I need to send a multipart form data, from NAV2017
The form is very easy...
<FORM action="https://cot.test.arba.gov.ar/TransporteBienes/SeguridadCliente/presentarRemitos.do" enctype="multipart/form-data" method="POST" name="form">
<P><LABEL for="user">User: </LABEL>
<input type="text" name="user" value="" /><BR/>
<LABEL for="pass">Pass: </LABEL>
<input type="password" name="password" value="" /></P>
<input type="file" name="file" value="" /><BR/>
<input type="submit" />
</FORM>
So, in NAV I have this code (var type DotNet=> System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')...
Url:='https://cot.test.arba.gov.ar';
Method:='/TransporteBienes/SeguridadCliente/presentarRemitos.do';
HttpClient := HttpClient.HttpClient();
HttpClient.BaseAddress := Uri.Uri(Url);
myFile.TEXTMODE(TRUE);
myFile.WRITEMODE(FALSE);
myFile.OPEN('C:\Users\Public\Documents\TB_30711170XXX_000002_20190621_000001.txt');
myFile.CREATEINSTREAM(myIS);
HttpMultipartFormDataContent:=HttpMultipartFormDataContent.MultipartFormDataContent();
stringusuario:=HttpStringContent.StringContent('30711170XXX');
stringpassword:=HttpStringContent.StringContent('711XXX');
filestream:=HttpStreamContent.StreamContent(myIS);
HttpMultipartFormDataContent.Add(stringusuario,'user');
HttpMultipartFormDataContent.Add(stringpassword,'password');
HttpMultipartFormDataContent.Add(filestream,'file','C:\Users\Public\Documents\TB_30711170XXX_000002_20190621_000001.txt');
HttpResponseMessage := HttpClient.PostAsync(Method,HttpMultipartFormDataContent).Result;
varResult := HttpResponseMessage.Content.ReadAsStringAsync.Result;
MESSAGE(varResult);
The result is ERROR 87: "the multipart form submitted is incorrect"
I think the problem is that every time I invoke "HttpMultipartFormDataContent.Add" a new section of the form is generated (so in my code, I would have 3 instead of just 2)
Someone help me, please.
The form is very easy...
<FORM action="https://cot.test.arba.gov.ar/TransporteBienes/SeguridadCliente/presentarRemitos.do" enctype="multipart/form-data" method="POST" name="form">
<P><LABEL for="user">User: </LABEL>
<input type="text" name="user" value="" /><BR/>
<LABEL for="pass">Pass: </LABEL>
<input type="password" name="password" value="" /></P>
<input type="file" name="file" value="" /><BR/>
<input type="submit" />
</FORM>
So, in NAV I have this code (var type DotNet=> System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')...
Url:='https://cot.test.arba.gov.ar';
Method:='/TransporteBienes/SeguridadCliente/presentarRemitos.do';
HttpClient := HttpClient.HttpClient();
HttpClient.BaseAddress := Uri.Uri(Url);
myFile.TEXTMODE(TRUE);
myFile.WRITEMODE(FALSE);
myFile.OPEN('C:\Users\Public\Documents\TB_30711170XXX_000002_20190621_000001.txt');
myFile.CREATEINSTREAM(myIS);
HttpMultipartFormDataContent:=HttpMultipartFormDataContent.MultipartFormDataContent();
stringusuario:=HttpStringContent.StringContent('30711170XXX');
stringpassword:=HttpStringContent.StringContent('711XXX');
filestream:=HttpStreamContent.StreamContent(myIS);
HttpMultipartFormDataContent.Add(stringusuario,'user');
HttpMultipartFormDataContent.Add(stringpassword,'password');
HttpMultipartFormDataContent.Add(filestream,'file','C:\Users\Public\Documents\TB_30711170XXX_000002_20190621_000001.txt');
HttpResponseMessage := HttpClient.PostAsync(Method,HttpMultipartFormDataContent).Result;
varResult := HttpResponseMessage.Content.ReadAsStringAsync.Result;
MESSAGE(varResult);
The result is ERROR 87: "the multipart form submitted is incorrect"
I think the problem is that every time I invoke "HttpMultipartFormDataContent.Add" a new section of the form is generated (so in my code, I would have 3 instead of just 2)
Someone help me, please.
0
Comments
-
Hopefully I can still help someone with this:
Not that this is useful for you guiyote, but as for me 5 years later I was twisting my brains for this. I needed to send multipart formdata over an HTTP webrequest in NAV 2017 (yes, people still use older versions). Turns out it is relatively easy (as it always seems when you find the solution).
I needed to send a text value and a json file. However, you can just build up your json with a stringbuilder. Below my code:
TempBlob.Blob.CREATEOUTSTREAM(OutStr);
OutStr.WRITETEXT(StringBuilder.ToString()); // StringBuilder contains my json text which I built up in code
TempBlob.Blob.CREATEINSTREAM(InStr);
HttpClient := HttpClient.HttpClient;
HttpRequestMessage := HttpRequestMessage.HttpRequestMessage(HttpMethod.Post, 'https://xxxxx.xxxxx.com/xxx/x/xxxxxx/files/');
// Add your headers
HttpRequestMessage.Headers.Add('Accept', 'application/json');
HttpRequestMessage.Headers.Add('Authorization', 'Bearer ' + APISetup.Token);
//Instantiate the MultipartFormDataContent
MultipartFormDataContent := MultipartFormDataContent.MultipartFormDataContent();
//Add the first string parameter here.
//Important to store it in StreamContent, otherwise the request fails as the content is already disposed then
StreamContent := StreamContent.StreamContent(InStr);
MultipartFormDataContent.Add(StreamContent, 'ParameterName1', 'TestFile.json');
//Add the second string parameter here.
//Important to store it in StringContent, otherwise the request fails as the content is already disposed then
StringContent := StringContent.StringContent('XXXXXX');
MultipartFormDataContent.Add(StringContent, 'ParameterName2');
HttpRequestMessage.Content(MultipartFormDataContent);
HttpResponseMessage := HttpClient.SendAsync(HttpRequestMessage).Result;
HandleText := HttpResponseMessage.Content.ReadAsStringAsync().Result;
IF NOT HttpResponseMessage.IsSuccessStatusCode THEN
ERROR(SendAssortmentFilesErr, HandleText)
ELSE
IF GUIALLOWED() THEN
MESSAGE(HandleText);
The result should contained in HandleText. The variables used here are:
Name DataType Subtype Length
MultipartFormDataContent DotNet System.Net.Http.MultipartFormDataContent.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
StringContent DotNet System.Net.Http.StringContent.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
StreamContent DotNet System.Net.Http.StreamContent.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
HttpClient DotNet System.Net.Http.HttpClient.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
HttpRequestMessage DotNet System.Net.Http.HttpRequestMessage.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
HttpMethod DotNet System.Net.Http.HttpMethod.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
HttpResponseMessage DotNet System.Net.Http.HttpResponseMessage.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
HttpHeader DotNet System.Net.Http.Headers.AuthenticationHeaderValue.'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
HandleText Text0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions