0
votes

I need to recreate this request I made in Postman with C#, I found that the HttpClient class solves most of my problems, but this time I couldn't solve it on my own. I embbeded an image with an example of the very post request.

POST REQUEST IN POSTMAN

There are three text paramethers and one file I need to send, with a content-type of form-data, the file needs to be a .json.

I tried constructing the POST request in many ways; this is my last version:

            string endpoint = $"{Endpoint}/captcha";
            string token_paramsJSON = JsonConvert.SerializeObject(v3Request.token_params);
            Hashtable ParametrosPOSTCaptcha = GetV3POSTParams(v3Request);

            UnicodeEncoding uniEncoding = new UnicodeEncoding();
            using (Stream ms = new MemoryStream()) {
                var sw = new StreamWriter(ms, uniEncoding);
                sw.Write(token_paramsJSON);
                sw.Flush();
                ms.Seek(0, SeekOrigin.Begin);

                using (MultipartFormDataContent form = new MultipartFormDataContent())
                {
                    form.Add(new StringContent(v3Request.username), "username");
                    form.Add(new StringContent(v3Request.password), "password");
                    form.Add(new StringContent(v3Request.type.ToString()), "type");
                    form.Add(new StreamContent(ms));

                    var response = await _httpClient.PostAsync(endpoint, form);
                    string ResponseTest = await GetResponseText(response);
                }
            }

With this code, I successfully establish a connection with the endpoint, send the username and password. But the response differs from the one I get with Postman using the same paramethers:

Postman: x=0&xx=1892036372&xxx=&xxxxx=1

The actual response I get is this:

HttpClient: {"error": "not-logged-in"}

Thanks in advance!

1

1 Answers

0
votes

Finally, I could solve it using the following implementation:

string endpoint = $"{Endpoint}/endpointName";
        string token_paramsJSON = JsonConvert.SerializeObject(v3Request.token_params, Formatting.Indented);
        Dictionary<string,string> PostParams = GetPOSTParams(v3Request);

        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, endpoint);

        UnicodeEncoding uniEncoding = new UnicodeEncoding();
        using (MultipartFormDataContent form = new MultipartFormDataContent())
        {
            foreach(var field in PostParams)
            {
                StringContent content = new StringContent(field.Value);
                content.Headers.ContentType = null;
                form.Add(content, field.Key);
            }
            var JsonFile = new StringContent(token_paramsJSON);
            JsonFile.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            JsonFile.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                Name = "\"token_params\"",
                FileName = "\"token.json\""
            };
            form.Add(JsonFile);
            request.Content = form;
            var response = await _httpClient.SendAsync(request, CancellationToken.None);
            return await GetCaptchaFromResponse(response);
        }