How can I post something on a html page with RestSharp

Eric Bezemer 196 Reputation points
2021-02-18T14:22:21+00:00

I use a POST and GET method, but I only the get method is working fine. I get a error code with my POST method "StatusCode: MethodNotAllowed, Content-Type:, Content-Length:0) I did try a lot of things but my first step would be to change or make text line on the webpage, but Iam stuck for now can anyone give some advice?

This is my POST code:

RestClient restClient = new RestClient("mywebpage");
        restClient.Authenticator = new HttpBasicAuthenticator("username", "password");
        RestRequest restRequest = new RestRequest("/rest/api/content/345345", Method.POST, DataFormat.Json);
        restRequest.AddJsonBody(new Root { title = "Whatever I wanted to say" });
        restRequest.AddHeader("accept", "application/json");

        IRestResponse restResponse = restClient.Execute(restRequest);

This is my GET code:

RestClient restClient = new RestClient("mywebpage");
        restClient.Authenticator = new HttpBasicAuthenticator("username", "password");
        RestRequest restRequest = new RestRequest("", Method.GET);


        IRestResponse restResponse = restClient.Execute(restRequest);
        Console.WriteLine(restResponse.Content); //The result I get the JSON code

JSON structure:

{"id":"","type":"page","status":"current","title":"PAGE","space":{"id":,"key":"","name":"","type":"global","_links":{"webui":"/display/","self":"mywebpage"},"_expandable":{"metadata":"","icon":"","description":"","homepage":"/rest/api/content/80147610"}},"history":{"latest":true,"createdBy":{"type":"known","username":"myname","userKey":"userkey","profilePicture":{"path":"/images/icons/profilepics/default.svg","width":48,"height":48,"isDefault":true},"displayName":"","_links":{"self":"mywebpage"},"_expandable":{"status":""}},"createdDate":"2021-02-17T08:36:54.443+01:00","_links":{"self":"mywebpage"},"_expandable":{"lastUpdated":"","previousVersion":"","contributors":"","nextVersion":""}},"version":{"by":{"type":"known","username":"","userKey":"8a8983ae68e05b250168e13","profilePicture":{"path":"/images/icons/profilepics/default.svg","width":48,"height":48,"isDefault":true},"displayName":"myName","_links":{"self":"mywebpage"},"_expandable":{"status":""}},"when":"2021-02-17T08:36:54.443+01:00","message":"","number":1,"minorEdit":false,"hidden":false,"_links":{"self":"mywebpage"},"_expandable":{"content":"/rest/api/content/"}},"extensions":{"position":"none"},"_links":{"webui":"/display/","edit":"/pages/resumedraft.action?draftId=78978","tinyui":"/x/GVllC","collection":"/rest/api/content","base":"","context":"","self":"mywebpage"},"_expandable":{"container":"/rest/api/","metadata":"","operations":"","children":"/rest/api/content/789345/child","restrictions":"/rest/api/content/1234324/r","ancestors":"","body":"","descendants":"/rest/api/content/"}}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,185 questions
{count} votes

Accepted answer
  1. Michael Taylor 55,841 Reputation points
    2021-02-19T14:45:33.447+00:00

    "I have pages with documentation on Atlassian Confluence platform. I want to make the pages dynamic so my idea is that I run a C# script every week that keeps this information up to date with information from a database."

    Caveat: I really believe that Confluence already has this functionality built in or can build an addon to do it. This is the correct approach but I know nothing about that platform to tell you how. I would strongly recommend you post in their forums for assistance. The answer below is for an arbitrary site.

    You aren't trying to automate a page at all then. You want to build a web app to retrieve the page contents from a database so you can update the database and the page shows the date. You cannot use Selenium for this as the changes would only impact the current session. You must change the data at the source.

    That is exactly what APIs are for. You need to modify the HTML and JS running your page though. You didn't specify what JS client library you're using, if any. This will have a dramatic impact on things. For a typical web app you could do the following (again, Confluence is a platform so you have to implement it their way, go to their docs):

    1) On the web server code side create a new API controller and action to return the data to be shown. If you want to be able to automate many different pages then you'll need some sort of key system to allow the HTML to identify the data to retrieve.
    2) In the API, given the key, retrieve the HTML from the database and return to the client. For performance reasons you probably want to cache this data.
    3) On the HTML side in place of hard coded text put in place holder divs. For each div that you want to retrieve the data from use JS to call back into your API to retrieve the HTML to be shown and then render it. If you're using Angular or Knockout or any # of client libraries then a simple binding attribute works. If you're not using any of these then JQuery can do it as well.
    4) Consider putting a loading gif of some sort in while you're making the API call(s) as browsers can only make a couple API calls at a time and it could take a while.
    5) SECURITY RISK: This is a security risk though as a malicious user could do a man in the middle attack to allow them to inject their own HTML into your page instead. Because of this most libraries require that you indicate the text being rendered contains HTML otherwise the HTML aspects are ignored.

    Alternatively, for Confluence, consider building an extension that handles all this for you and drop the extension on the page. The platform is extensible as you can see all the existing extensions available. You should read their docs to see if you can build the same thing. This would likely be easier to maintain long term over an API and would certainly be more secure. However you'll need to contact them for support on that.


1 additional answer

Sort by: Most helpful
  1. Michael Taylor 55,841 Reputation points
    2021-02-18T15:17:10.64+00:00

    Firstly I should clarify that using REST API calls have nothing to do with a webpage. REST makes HTTP calls like a web page does but has nothing to do with filling out any sort of web page nor can you do so with HTTP. The only thing you can do is see what POST/PUT/etc request a web page is doing and emulate it without the webpage by making the corresponding HTTP call. REST is not for automating the interaction of a web page.

    The error you're getting indicates that the URL you are trying to send to does not support POST. If you're trying to emulate what a webpage is doing then use the F12 tools in the browser then go to the Network tab. Navigate to the page you want (this gives you the GET requests you need to make to get the corresponding data, if any). Then enter any data you need and click whatever button you want to replicate in REST. That will trigger the POST/PUT/etc call to a specific URL. The network tab in the browser shows you all this information. Use that to build the corresponding HTTP request in REST. Note that webpages tend to use forms to post data so you need to ensure you're using the corresponding content-type as well.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.