Ubnik
Ubnik

Reputation: 23

Generating Multipart form data body from text for RestSharp request

I'm trying to get a basic login feature working using RestSharp. I have the login user and password as text that I need to convert somehow since the server accepts requests as multipart/form-data and my initial attempt was this:

   RestRequest request = new RestRequest(resource, Method.POST);

   request.AddParameter("login", config.Login, ParameterType.RequestBody);
   request.AddParameter("pass", config.Password, ParameterType.RequestBody);

Only to find out I cannot add two params for request body, where it discards all but the first. So my next attempt was to build the requestBody:

        MultipartFormDataContent formData = new MultipartFormDataContent();
        formData.Add(new StringContent(config.Login), "login");
        formData.Add(new StringContent(config.Password), "pass");
        request.AddBody(formData);

This also didnt work and just returned the closed element </MultipartFormDataContent>

Last I tried to just pass them as post params rather than the body, which it seems to have just ignored considering it expects a form I guess:

        request.AddParameter("login", JsonConvert.SerializeObject(config.Login), ParameterType.GetOrPost);
        request.AddParameter("pass", JsonConvert.SerializeObject(config.Password), ParameterType.GetOrPost);

I'm looking for any tips on how I can somehow convert and send this text as valid form-data. It looks easier to do with HTTPWebRequest rather than RestSharp but it's bothering me that I'm sure it's possible.

Thanks so much!

Upvotes: 0

Views: 3191

Answers (1)

Ubnik
Ubnik

Reputation: 23

It turns out that the default headers generated by RestSharp were sending the boundary parameter with quotes, where my endpoint was receiving them without quotes, so I needed a lower level interface and ended up being able to create a custom header for boundary with HttpWebRequest and MediaHeadervalue that fit my needs. Thanks everyone for looking!

Upvotes: 1

Related Questions