-1
var client = new RestClient(Url.Combine(sharepointSiteUrl, "_api"));
client.CookieContainer = new CookieContainer();
client.CookieContainer.SetCookies(siteUri, cred.GetAuthenticationCookie(siteUri));

var digestReq = new RestRequest("contextinfo", Method.POST);
digestReq.AddHeader("Accept", "application/json");
var digest = client.Execute<dynamic>(digestReq).Data;

I am not familiar with C# POST calls. I am trying to convert this code to Python and wanted to understand how to do it.

I tried looking up requests in Python but i do not understand how cookies work there and how to execute these calls.

user11717481
  • 1
  • 9
  • 15
  • 25
  • https://stackoverflow.com/questions/7164679/how-to-send-cookies-in-a-post-request-with-the-python-requests-library – Jisson Nov 14 '22 at 06:00
  • @Jisson I understand this line var digestReq = new RestRequest("contextinfo", Method.POST); can be converted to digestReq = requests.post("contextinfo") but dont understand the two lines later. digestReq.AddHeader("Accept", "application/json"); var digest = client.Execute(digestReq).Data; – pulseozara Nov 14 '22 at 06:05
  • I believe its the request object( method is POST), Instead of converting C# code into line by line. just do the following, import requests, cookies = {'some_key': '12345678'} resp =requests.post('the url', cookies=cookies) – Jisson Nov 14 '22 at 06:10
  • how about the header and what does contextinfo mean? – pulseozara Nov 14 '22 at 06:15
  • I don't have C# knowledge, you can't find line by line replacement of a c# code to python. – Jisson Nov 14 '22 at 06:20
  • 1
    The `accept` header is a common one - you can look up what it's used for. If you don't know about headers, then perhaps a bit of reading on how HTTP works would help: https://developer.mozilla.org/en-US/docs/Web/HTTP – ndc85430 Nov 14 '22 at 06:53
  • In any case, you do need to understand how both libraries work to make HTTP requests with them. In your example code in comments, you were using "contextinfo" as the URL in the call to `requests.post` - I doubt that's what you wanted. – ndc85430 Nov 14 '22 at 06:55
  • 1
    contextinfo: If `sharepointSiteUrl` is `https://www.example.com/` then I would expect the client initialization line combined with "contextinfo" to result in a POST request to `https://www.example.com/_api/contextinfo`. – ProgrammingLlama Nov 14 '22 at 06:57

1 Answers1

0

Use the following lines of code to send a post request to a server resource

import requests  # you need to install requests library  (pip install requests)
my_cookie = {'some_key': 'value}
resp = requests.post('the url', cookies=cookies)  # request.get
print(resp.status_code) # print the http response code (200, 400, 500 etc)
print(resp.text)        # shows the text format of the response
print(resp.json())      # if the data returned by the server is json

Variable resp will have the response from the server. use it to read the data returned by the server.

Jisson
  • 3,566
  • 8
  • 38
  • 71