Archived Forum Post

Index of archived forum posts

Question:

How to send multipart/form-data using REST API ?

Apr 30 '17 at 13:11

I'm trying to send a multipart/form-data using the REST API. For HTTP there is an example available, but I'm bound to use the REST API...

Random rnd = new Random();
string boundary = "-----------------------------" + rnd.Next(100000, 999999).ToString() + rnd.Next(10000, 99999).ToString();
rest.AddHeader("Content-Type", "multipart/form-data; boundary=" + boundary);

string postString = "";
foreach (httpParam h in httpParams)
{
    postString += boundary + "\r\n";
    postString += "Content-Disposition: form-data; name=\"" + h.name + "\"" + "\r\n";
    postString += "\t" + "\r\n";
    postString += h.value + "\r\n";
}
postString += boundary + "--";
rest.SetMultipartBodyString(postString);
success = rest.SendReqMultipart("POST", URL.PathWithQueryParams);

Since I get the error "Not a multipart content-type." I guess the SendReqMultipart is incorrect... but how to?


Accepted Answer

Sorry for the trouble. This new build should fix it:

https://chilkatdownload.com/prerelease/chilkatdotnet46-9.5.0-win32.zip
https://chilkatdownload.com/prerelease/chilkatdotnet46-9.5.0-x64.zip

The problem was that Chilkat expected each sub-part to have a Content-Type header, and failed the rendering of the request when one did not exist. I eliminated that requirement. (Also, the reason for the error should've been reported in the LastErrorText.)


Answer

Here's an example:

https://www.example-code.com/csharp/rest_multipart_form_data.asp


Answer

Thank you for your quick reply. However after hours of trying I have failed to get it to work...

Chilkat.Global glob = new Chilkat.Global();
bool success = glob.UnlockBundle("Anything for 30-day trial");

Chilkat.Rest rest = new Chilkat.Rest();
rest.VerboseLogging = true;

success = rest.Connect("127.0.0.1", 80, false, true);
if(!success)
{
    string blaat = rest.LastErrorText;
    return;
}
rest.AddHeader("Content-Type", "multipart/form-data");

rest.PartSelector = "1";
rest.AddHeader("Content-Disposition", "form-data; name=\"text1\"");
rest.SetMultipartBodyString("text 123 abc");

bool k = rest.SendReqMultipart("POST", "/post.php");
string L = rest.LastErrorText;

bool m = rest.SendReqNoBody("GET", "/post.php");
string n = rest.LastErrorText;
rest.ClearAllHeaders();
rest.ClearAllParts();
rest.PartSelector = "";
return;

With a breakpoint on both "returns". I'm running apache locally, and created the file "post.php" with the content:

<?PHP
file_put_contents( './' . time() . '.txt', file_get_contents('php://input') );
?>

Going to 127.0.0.1/post.php in the browser, created an empty file in my http directory. When running above script:

k = false;
L = ChilkatLog:
  SendReqMultipart:
    DllDate: Mar 23 2017
    ChilkatVersion: 9.5.0.66
    UnlockPrefix: *REMOVED*
    Architecture: Little Endian; 64-bit
    Language: .NET 4.6 / x64
    VerboseLogging: 1
    sendReqMultipart:
      renderMultipartBody:
        boundary: ------------000905020102050806050708
      --renderMultipartBody
    --sendReqMultipart
    Failed.
  --SendReqMultipart
--ChilkatLog
m = true
n = ChilkatLog:
  SendReqNoBody:
    DllDate: Mar 23 2017
    ChilkatVersion: 9.5.0.66
    UnlockPrefix: *REMOVED*
    Architecture: Little Endian; 64-bit
    Language: .NET 4.6 / x64
    VerboseLogging: 1
    sendReqNoBody:
      sendReqHeader:
        uriPath: /post.php
        constructStartLine:
          uriPath: /post.php
        --constructStartLine
        getMimeHeaderHttp:
          headerField: Host: 127.0.0.1
        --getMimeHeaderHttp
        requestHeader: [GET /post.php HTTP/1.1
Host: 127.0.0.1

]
      --sendReqHeader
    --sendReqNoBody
    Success.
  --SendReqNoBody
--ChilkatLog

When breaking on L, rest.LastRequestHeader = "" and rest.LastRequestStartLine = "".

Since the 2nd request works, an empty file is created in the http dir. The same behaviour and errors are seen when using FullRequestMultipartAsync instead of SendReqMultipart. Am I doing it wrong? (I quite literally copied the code...) or is there a bug in SendReqMultipart?