0

Finaly figure out how to send QueryString as POST via HttpClient but another problem, URi string is too long becosue one of the string is file encoded to ToBase64String

Is posible to convert this solution:

NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString(string.Empty);

queryString.Add("mail_from", FromEmailAddress);
queryString.Add("mail_to", ToEmailAddress);
queryString.Add("mail_Attachment", ZipInBytes);

var response = await client.PostAsync($"/api?{queryString}", null);

Is there any other way how to send very long string? In postman working JSON raw data send to API

{
     "mail_from":"value",
     "mail_to":"value2,
     "mail_Attachment":"very long string"
}

or I'm completely out and its not possible. My goal is send data with file from Outlook to API and save to database.

novice
  • 321
  • 4
  • 19
  • I am not familliar with postman do you have the Raw http request send from postman? I suspect "raw data" option to be in the body. – xdtTransform Oct 13 '20 at 12:24
  • You can change the maximum query length on the web server but i highly recommend not to unless you don't care about DDOS attacks. I would recommend to use an alternate method that `POST` specially for large data transfer. There are many other ways to achieve this other than using `POST` – Franck Oct 13 '20 at 12:26
  • related: https://stackoverflow.com/questions/25158452/how-to-send-a-post-body-in-the-httpclient-request-in-windows-phone-8 – xdtTransform Oct 13 '20 at 12:26
  • @xdtTransform ````request.AddParameter("application/json", "{\r\n \"mail_from\":\"mail@mail.com\",\r\n \"mail_to\":\"mailfrom@mail.com\"\r\n}", ParameterType.RequestBody);```` can be placed in this code ````variable```` insteed ````mail@mail.com```` ? – novice Oct 13 '20 at 12:34

1 Answers1

2

You should (if possible) send data such as files in the body, not the QueryString.

For instance:

//Class containing all POST data
public class PostBody{
   public string FromEmailAddress{get;set;}
   public string ToEmailAddress{get;set;}
   public string ZipInBytes{get;set;}
}

// Populate model
PostBody body = new PostBody{ FromEmailAddress = FromEmailAddress, ToEmailAddress = ToEmailAddress, ZipInBytes = ZipInBytes};

// Serialize to JSON
var serializedBody = JsonConvert.SerializeObject(body);

// Set content type
client.Headers.Add("Content-Type", "application/json");

// Do request with serialized JSON as post body
var response = await client.PostAsync($"/api?{queryString}", serializedBody);
Paaz
  • 186
  • 7