mr_blond
mr_blond

Reputation: 1730

What the difference between this Post webrequest and similar request in Postman?

I want to make Post request to "https://sslecal2.forexprostools.com/ajax.php". So there is my code:

        string URI = "https://sslecal2.forexprostools.com/ajax.php";
        string requestBody = String.Format("{{\"dateFrom\": \"{0}\", \"dateTo\": \"{1}\", \"timeZone\": {2}, \"action\": \"{3}\"}}",
                                           "2018-12-24", "2018-12-24", 18, "filter"); //json format

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URI);  //make request         
        request.Method = "POST";
        request.UserAgent = "";
        request.Headers.Add("X-Requested-With", "XMLHttpRequest");
        using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
        {
            writer.Write(requestBody); //write your request payload
        }

        WebResponse response = request.GetResponse();
        string jsonData = String.Empty;

        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            jsonData = reader.ReadToEnd();
        }
        response.Close();

I made something not correct in "requestBody" in string " string requestBody = String.Format("{{\"dateFrom\"..." because I get 200 and empty html answer.

And I attach the screens of the same request in postman with html code in answer. This request in postman processes well.

What the difference between this Post webrequest and request in Postman? headers

body with answer

Upvotes: 0

Views: 1107

Answers (2)

Caius Jard
Caius Jard

Reputation: 74700

In PostMan, if you click the "Code" in the top right, under the send button, you can choose C# (RestSharp).. If you're not using RestSharp, there's a small amount of work to do to convert it to something else, but the basics are all there.

Here's the autogen output for your case (RestSharp):

var client = new RestClient("https://sslecal2.forexprostools.com/ajax.php");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "bfd1a3b3-983f-4160-a091-6f0962413e58");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("X-Requested-With", "XMLHttpRequest");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("undefined", "dateFrom=2018-01-24&dateTo=2018-01-24&timeZone=18&action=filter", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

Converting it to HttpWebRequest requires:

  • AddHeader -> Headers.Add
  • Specify method
  • Body data is set differently - take PostMan's string and write it to the request stream

Or install RestSharp free from NuGet

Upvotes: 0

Renatas M.
Renatas M.

Reputation: 11820

With postman you posting different format data. To get same thing in code you need to change request body format and set content type of request:

string URI = "https://sslecal2.forexprostools.com/ajax.php";
string requestBody = String.Format("dateFrom={0}&dateTo={1}&timeZone={2}&action={3}",
"2018-12-24", "2018-12-24", 18, "filter"); //<-- Change this

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URI); 
request.Method = "POST";
request.UserAgent = "";
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
request.ContentType = "application/x-www-form-urlencoded"; //<-- Add this
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
    writer.Write(requestBody); 
}

WebResponse response = request.GetResponse();
string jsonData = String.Empty;

using (var reader = new StreamReader(response.GetResponseStream()))
{
    jsonData = reader.ReadToEnd();
}
response.Close();

Upvotes: 1

Related Questions