how to send HTML multipart form

guiyoteguiyote Member Posts: 12
edited 2019-12-17 in NAV Three Tier
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&quot; 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.

Comments

  • FlorisFloris Member Posts: 2
    edited 2024-06-07
    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 Text
Sign In or Register to comment.