1

.wln messages import returns error 4

(edited by Karnaugh 10/11/2017 11:20:53)

Topic: .wln messages import returns error 4

Hello Gurtam,
I am currently trying to import .wln messages from the api, as described in https://sdk.wialon.com/wiki/en/kit/remo … t_messages
The message format I adhere to is as described in https://sdk.wialon.com/wiki/en/kit/remo … t/messages
Now, I've double and triple checked the Post request I send as well as the upload file. As a proof there are two upload zip files attached, one with the simplest message entry possible, and one with two message entries with a bit more data substance. I've also tried to upload them via the Wialon hosting UI, quite successfully. The post request headers and content are set exactly as specified in the documentation, nevertheless here is the c# code that does that.

            
           using (Stream fs = await file.OpenAsync(PCLStorage.FileAccess.Read))
            {
                var streamContent = new StreamContent(fs);
                streamContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/zip");
                using (var client = new HttpClient())
                using (var formData = new MultipartFormDataContent())
                {
                    
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text / html,application / xhtml + xml,application / xml; q = 0.9,*/*;q=0.8");
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Charset", "windows-1251,utf-8;q=0.7,*;q=0.3");
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Language", "ru,en-US;q=0.8,en;q=0.6");
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate, sdch");

                    formData.Add(streamContent, "messages_filter_import_file", file.Name);
                    formData.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
                    var response = await client.PostAsync(url, formData);
                    if (!response.IsSuccessStatusCode)
                    {
                        return null;
                    }

                    return await response.Content.ReadAsStringAsync();
                }
            }

I also attach the sniffed http communication from my device to the remote API, which exactly mimics your example.
Now, always getting the impossible to debug on my side error 4, could you give me a hint what might possibly be going wrong.

Post's attachments

Attachment icon msg-76d9b220-c86b-482f-a5ed-451c7adfa33b.zip 206 b, 192 downloads since 2017-11-10 

Attachment icon msg-79fa4801-fbcf-4711-b1f4-e194682f1542.zip 283 b, 185 downloads since 2017-11-10 

2

.wln messages import returns error 4

Re: .wln messages import returns error 4

I can imagine that the case is not trivial or the issue is complex. What bothers me is that the support team redirect me here and claim they have basic and insufficient knowledge. Here no one from Gurtam bothers to answer. As for my part the case is as specific and documented as it can be and the procedure I follow is exactly as specified in the documentation. I've been trained for customer support position at Progress three years ago and the assistance I get from Gurtam has nothing in common with the practices I've followed. Ten days with an open case, possibly linked to incomplete documentation. How could this happen in a supposedly well established company?

3

.wln messages import returns error 4

Re: .wln messages import returns error 4

Karnaugh, hello. Sorry for long waiting of answer.
Error 4 you can get for this request only if file was not sent or sent with wrong parameters.
Your headers and content have some differencies with browser's ones:

1. content-type:multipart/form-data; boundary=----WebKitFormBoundarySpQSQhs0MtJaVfeg - no boundary was set
2. Content-Disposition: form-data; name="messages_filter_import_file"; filename="kind.zip" - name and filename in quotes
3. Content-Type: application/x-zip-compressed - insted of application/zip

Head of Wialon Local Department
Gurtam
4

.wln messages import returns error 4

Re: .wln messages import returns error 4

Hello deal,
Your answer was pretty informative and the problem was actually the boundary. Now that it is set, everything works fine. It does not need to be the chrome string, but I do not dare touch it now that it works. (superstition on the way...)
The Content-Type param was set to application/zip, because that's how it was in the documentation here - https://sdk.wialon.com/wiki/en/kit/remo … t_messages. So it needs an update to application/x-zip-compressed
I will paste the C# code that I use  in Xamarin, in case someone needs this to compose his own request.
Thanks for your cooperation now that the case is solved.

            using (Stream fs = await file.OpenAsync(PCLStorage.FileAccess.Read))
            {
                var streamContent = new StreamContent(fs);
                streamContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-zip-compressed");
                using (var client = new HttpClient())
                using (var formData = new MultipartFormDataContent("----WebKitFormBoundarySpQSQhs0MtJaVfeg"))
                {

                    client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text / html,application / xhtml + xml,application / xml; q = 0.9,*/*;q=0.8");
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Charset", "windows-1251,utf-8;q=0.7,*;q=0.3");
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Language", "ru,en-US;q=0.8,en;q=0.6");
                    client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate, sdch");

                    formData.Add(streamContent, "\"messages_filter_import_file\"", $"\"{file.Name}\"");
                    formData.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=----WebKitFormBoundarySpQSQhs0MtJaVfeg");
                    var response = await client.PostAsync(url, formData);
                    if (!response.IsSuccessStatusCode)
                    {
                        return null;
                    }

                    return await response.Content.ReadAsStringAsync();
                }
            }
5

.wln messages import returns error 4

Re: .wln messages import returns error 4

I have testing with example. But not working.

// Convert the data array into URL Parameters like a=b&foo=bar etc.
        $data = http_build_query($data);
       
        // parse the given URL
        echo $url;
        $url = parse_url($url);
       
        print_r(    $url );
        //if ($url['scheme'] != 'http') {
        //    die('Error: Only HTTP request are supported !');
        //}
       
        // extract host and path:
        $host = $url['host'];
        $path = $url['path'];
       
        // open a socket connection on port 80 - timeout: 30 sec
        $fp = fsockopen($host, 80, $errno, $errstr, 30);
        echo $path.'<br/>';
        if ($fp){
           
            // send the request headers:
            fputs($fp, "POST $path HTTP/1.1\r\n");
            fputs($fp, "Host: $host\r\n");
           
            if ($referer != '')
            fputs($fp, "Referer: $referer\r\n");
            fputs($fp, "url:".$url."\r\n\r\n");
            //fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
            //fputs($fp, "Content-length: ". strlen($data) ."\r\n");
            //fputs($fp, "Connection: close\r\n\r\n");
            fputs($fp, "Connection: keep-alive\r\n\r\n");
            fputs($fp, "Content-Length: 2744\r\n\r\n");
            fputs($fp, "Cache-Control: no-cache\r\n\r\n");
            fputs($fp, "Content-Type: multipart/form-data; boundary=----WebKitFormBoundarySpQSQhs0MtJaVfeg");
            fputs($fp, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n\r\n");
            fputs($fp, "Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.3\r\n\r\n");
            fputs($fp, "Accept-Encoding: gzip,deflate,sdch\r\n\r\n");
            fputs($fp, "Accept-Language: ru,en-US;q=0.8,en;q=0.6\r\n\r\n");
            fputs($fp, "------WebKitFormBoundarylvunQiir9AesO8qB\r\n\r\n");
            fputs($fp, 'Content-Disposition: form-data; name="params"\r\n\r\n');
            fputs($fp, '{"itemId":17928018,"eventHash":"jUploadForm'.time().'}\r\n\r\n');
            fputs($fp, "------WebKitFormBoundarylvunQiir9AesO8qB\r\n\r\n");
            fputs($fp, 'Content-Disposition:   form-data; name="messages_filter_import_file"; filename="17928018.zip"');
            fputs($fp, "------WebKitFormBoundarylvunQiir9AesO8qB\r\n\r\n");
            //fputs($fp, 'Content-Disposition: form-data; name="messages_filter_import_file"; filename="17928018.wln"\r\n\r\n');
            fputs($fp, "Content-Type: application/zip\r\n\r\n");
            fputs($fp, "------WebKitFormBoundarylvunQiir9AesO8qB--\r\n\r\n");
           
           
           
            fputs($fp, $data);
           
            $result = '';
            while(!feof($fp)) {
                // receive the results of the request
                $result .= fgets($fp, 128);
            }
        }
        else {
            return array(
            'status' => 'err',
            'error' => "$errstr ($errno)"
            );
        }
       
        // close the socket connection:
        fclose($fp);
       
        // split the result header from the content
        $result = explode("\r\n\r\n", $result, 2);
       
        $header = isset($result[0]) ? $result[0] : '';
        $content = isset($result[1]) ? $result[1] : '';
       
        // return as structured array:
        return array(
        'status' => 'ok',
        'header' => $header,
        'content' => $content
        );



{"error":4,"reason":"WRONG_PARAMS"}

6

.wln messages import returns error 4

Re: .wln messages import returns error 4

Hi @Karnaugh ... I need to implement message import in VB.NET ... I am using your code (thank you!) ... Do you think it is ok as I wrote it?

filePath is the full path to my .zip file.

Dim urlx As String = Wialon_WS_URL + "?svc=exchange/import_messages&sid=" + WialonID
Using fs = New FileStream(filePath, FileMode.Open, FileAccess.Read)
    Dim streamContent As New StreamContent(fs)
    streamContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-zip-compressed")
    Dim client As New HttpClient()
    Using formData = New MultipartFormDataContent("----WebKitFormBoundarySpQSQhs0MtJaVfeg")
        client.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
        client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Charset", "windows-1251,utf-8;q=0.7,*;q=0.3")
        client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Language", "ru,en-US;q=0.8,en;q=0.6")
        client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate, sdch")
        formData.Add(streamContent, """messages_filter_import_file""", """" + Path.GetFileName(filePath) + """")
        formData.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data; boundary=----WebKitFormBoundarySpQSQhs0MtJaVfeg")
        Dim response As HttpResponseMessage = client.PostAsync(urlx, formData).Result
        If response.IsSuccessStatusCode Then
            Dim json As String = response.Content.ReadAsStringAsync().Result
            txtLog.Text += vbCrLf + json + vbCrLf
        End If
        Return
    End Using
End Using

I really appreciate your help! Thank you!