0

I am trying to send the data in POST API and tried the genrated code from postman. But the line "request.AlwaysMultipartFormData = true;" is giving error "RestRequest does not contain a definition for AlwaysMultipartFormData". If i remove the line, then I am facing the error "Invalid URI: The Uri string is too long."

var client = new RestClient("http://localhost:4000/xxx.php");
client.Timeout = -1;   
var request = new RestRequest(Method.POST);
**request.AlwaysMultipartFormData = true;** // had to remove
request.AddParameter("htmlstring", htmlstring);
IRestResponse response = client.Execute(request);
var base64 = response.Content;
return base64;
        
Sumithra
  • 3
  • 2
  • 1
    I think we're missing some code to get the error. We need to know if there are any parameters passed in (fake data that results in the error is enough). Also, consider the error might be coming from the PHP side. – pavlindrom Aug 26 '21 at 12:43
  • Thanks. But the difference is i can't add the "request.AlwaysMultipartFormData = true; " in c# which is there in postman. If i add the line , It is giving "RestRequest does not contain a definition for AlwaysMultipartFormData" error – Sumithra Aug 26 '21 at 13:02
  • The data deing tranfered is html string with base64 images – Sumithra Aug 26 '21 at 13:07
  • That's a build error, telling you there's no such property on RestRequest. From another answer it looks like calling AddFile will always set that for you: https://stackoverflow.com/a/45382624/2048017 – pavlindrom Aug 26 '21 at 13:11
  • Can you update the question with more code so that we know how you're forming the request? Everything down to `client.Execute`. – pavlindrom Aug 26 '21 at 13:15
  • I can't set AddFile because I am sending HTML string like " ... – Sumithra Aug 26 '21 at 13:20
  • @pavlindrom update the code – Sumithra Aug 26 '21 at 13:20
  • Why not put the htmlstring in the body? The error tells you the query string isn't the right place to do it. Then update the PHP side to get it from there. – pavlindrom Aug 26 '21 at 13:24
  • can you please provide a sample or reference to put the htmlstring in the body – Sumithra Aug 26 '21 at 13:26
  • As far as i know, usually the post request is made through AddParameter.. If a reference is provided , It will be much helpful – Sumithra Aug 26 '21 at 13:27
  • Set the `RequestFormat` property based on what you're expecting in PHP, and then call `AddBody` with the data (you can use an anonymous type). – pavlindrom Aug 26 '21 at 15:26
  • I added an answer with some more information I found from a bit more reading online. May need tweaking but you should be able to go from here. – pavlindrom Aug 26 '21 at 16:49

1 Answers1

0

Putting this in answer format to recap the discussion we had through comments, since the web server is telling you that the request is too long, you can send the same data through the POST body. I also changed the code a bit based on personal preference.

var client = new RestClient("http://localhost:4000/");
client.Timeout = -1;

var request = new RestRequest("xxx.php", Method.POST);
request.AddParameter("htmlstring", htmlstring, ParameterType.GetOrPost);

var response = client.Execute(request);
var base64 = response.Content;
return base64;

Then in PHP you can use:

$htmlstring = $_POST["htmlstring"];
pavlindrom
  • 366
  • 2
  • 12