0

I am using the following code to call a REST service using C#

 string PostData= @"{""name"":""TestName""}";

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://something.com:1234/content/abc.v1.json");

        request.Method = "POST";
        request.ContentLength = 0;
        request.ContentType = ContentType;
        request.Accept = "application/json";
        request.KeepAlive = false;
        request.CookieContainer = cookie;

        if (!string.IsNullOrEmpty(PostData) && Method == HttpVerb.POST)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);
            request.ContentLength = bytes.Length;
            request.AllowAutoRedirect = true;
            using (Stream writeStream = request.GetRequestStream())
            {
                writeStream.Write(bytes, 0, bytes.Length);
            }
        }

        try
        {  // Gets exception
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {

             ....
             }
        }

And I am getting an exception "400 bad request" on the line calling GetResponse(). The documentation of the service says the status code 400 means a required argument is missing. But as you can see the argument name (which is the only required argument ) is supplied with the request.

I tried to call the service with CURL and it successfully got executed.

curl -v -b cookie.txt -X POST -H "Content-Type: application/json" -d "{\"name\": \"TestName\"}" http://something.com:1234/content/abc.v1.json

So I assume there is something wrong with my C# code which doesn't seem to be passing the parameters. Any idea?

EDIT

Here is the relevant part of the documentation:

Method

POST

Headers

Content-Type: application/json

Body

The request body is made up of JSON containing the following properties:

Name : name Required : yes Type: string

Response Status codes

201 Created Success

400 Bad Request A required property is missing in the request body.

7
  • Consider using RestSharp; it's a fantastic interface for RESTful services. Commented Nov 19, 2013 at 13:20
  • @MichaelPerrenoud: Thankz but 3rd party services are not allowed Commented Nov 19, 2013 at 13:39
  • Third party services? Or do you mean third party libraries? And what is the actual constraint there? Commented Nov 19, 2013 at 13:40
  • I mean third party libraries Commented Nov 19, 2013 at 13:42
  • Have you tried sending the data as a string instead of encoded bytes? The StreamWriter class would allow that. See example here Commented Nov 19, 2013 at 14:06

2 Answers 2

1

That's not how data is sent in a POST request. It should look like this:

string PostData= "name=TestName";

If you have more than one value, you separate them with the & character. Example:

string PostData= "name=TestName&number=20";
Sign up to request clarification or add additional context in comments.

12 Comments

But the documentation says it should be sent in the json format. and btw, i had already tried the method u suggested and it also resulted in the same exception
I have added the relevant part of the documentation in the questiom
@Sharun: Then it's not really a REST service... Anyway, what's in your ContentType variable? Is the response coming with the content type application/json?
ContentType="application/json"
@Sharun: You have set the Accept header to application/json also, is that the content type of the response?
|
0

I suggest using the System.Net.Http HttpClient class.

        string PostData = @"{""name"":""TestName""}";

        var httpClientHandler = new HttpClientHandler();
        httpClientHandler.CookieContainer = cookies;
        var httpClient = new HttpClient(httpClientHandler);

        var content = new StringContent(PostData, 
            Encoding.GetEncoding("iso-8859-1"), "application/json");

        httpClient.PostAsync("http://something.com:1234/content/abc.v1.json", content);

1 Comment

@Sharun Microsoft support .NET 4 also. nuget.org/packages/Microsoft.Net.Http

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.