2

I have written a method to post messages to an uri.

public string RestClientPost(string uri, string message = null)
    {
        var client = new RestClient(uri);
        var request = new RestRequest(Method.POST);
        request.AddHeader("Accept", "text/xml");
        if (!string.IsNullOrEmpty(message))
            request.AddParameter(message, ParameterType.RequestBody);

        var result = "";
        var response = client.Execute(request);

        if (response.StatusCode == HttpStatusCode.OK)
        {
            result = response.Content;
            Console.WriteLine(result);
        }
        else
        {
            result = response.StatusCode.ToString();
        }


        return result;
    }

and below code is used above method to post.

public void test123()
    {
        string uri = "myuri"; //private uri, cannot expose.
        var file= System.IO.File.ReadAllText(Path.Combine(Settings.EnvValPath, "RestClientXML", "test.XML"));
        var content = new RestClientServices().RestClientPost(uri, file);

    }

however, it returns "Unsupported Media type".

my test.XML's content is

<customer> 
    <customerName>test</customerName > 
    <customerStatus>OK</customerStatus > 
</customer>

And using Advanced Rest Client Plugin for Google Chrome, I'm able to post it and return with string that I wanted. Is there something wrong?? I set "content-type" to "text/xml" in Advanced Rest Client.

  • The return message is id of the customer. e.g: 2132
1
  • I change my original plan from RestSharp to Rest Client C# which I can get what I want now. If you guys need the answers, just drop me a message. Commented Jul 17, 2013 at 7:34

2 Answers 2

1

im using postman,

postman get the c# code tools

if you can call any xml web services with this tools , then you can click on code and select restsharp and copy paste it to your code

Sign up to request clarification or add additional context in comments.

Comments

0

This happened because the header "Accept" is to specify a type of return object. In this case a value of a variable content, not the type of content to send. Specify a type of content to send with: "Content-Type: application/xml".

If a return type of POST request is a media file, you can use 'image/png' or 'image/jpeg'. You can use multiple accept header values like: "application/xml, application/xhtml+xml, and image/png". For example, you can use Fiddler to debug HTTP(s) traffic - it's a good tool for web developers.

2 Comments

The return value is a string. E.g: 2312. Can you show an example? I don't really understand your answer.
What @Joseph wanted to say is that 'Accept' header indicates the type of response that you expect (see Accept Header. So in this specific case you don't need it You should specify request format: request.RequestFormat = DataFormat.Xml

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.