0

I am required to connect to a HTTPS server and post data in the form of a Dictionary to it. The server API is created by my teacher and he has provided Python code on how to post to the server, using the Python library Reguests (see docs).

The Python template using Requests

def call_server(move): //Move is an integer value
   res = requests.post(SERVER_ADRESS,
                       data={
                           "id": ID, 
                           "move": move, 
                           "api_key": API_KEY,
                       })

where of course SERVER_ADRESS, ID, move and API_KEY all are provided.

I am trying to rewrite this functionality in C# using the HttpClient class. I have tried using the method PostAsync in HttpClient (see docs) by using both a StringConent and a FormUrlEncodedContent object as the HttpContent (can be seen below). However both methods results in the server responding with the error code 422 - Unprocessable Entity. Which apperently means

The server understands the content type and syntax of the request entity, but still server is unable to process the request for some reason.

My template using HttpClient

class HttpHandler
{
   static readonly HttpClient client = new HttpClient();
   static readonly string SERVER_ADRESS = ...
   static readonly string ID = ...
   static readonly string APIKey = ...
   Dictionary<string, string> data;
   
   public void callServer(int move)
   {
      data = new Dictionary<string, string>();
      data.Add("id", ID);
      data.Add("move", move.ToString());
      data.Add("api_key", APIKey);
      string serialized = JsonConvert.SerializeObject(data); //From Newtonsoft.Json
      HttpContent stringCont = new StringContent(serialized,Encoding.UTF8,"application/json");    
      HttpContent byteCont = new FormUrlEncodedContent(data);
            
      var resStringCont = client.PostAsync(SERVER_ADDRESS, stringCont);
      //var resByteCont = client.PostAsync(SERVER_ADDRESS, byteCont);
   } 
}

Anyone more experienced with Https that can figure out why I can't successfully post to the server?

7
  • Does this answer it? stackoverflow.com/questions/28468484/… Commented Feb 2, 2022 at 18:58
  • FWIW unless there is a certificate problem, which it doesn't seem to be, I doubt the issue is related to HTTPS Commented Feb 2, 2022 at 19:02
  • Have you checked the json? Does it look ok? Commented Feb 2, 2022 at 19:06
  • @PoulBak The Json looks fine! Commented Feb 2, 2022 at 20:26
  • Trying to find some documentation on python-requests .post method.... I can only guess that using data= will send a "application/x-www-form-urlencoded" content type. Commented Feb 4, 2022 at 1:07

1 Answer 1

1

Try using this approach, it works with the server I am sending data to.

var content = new StringContent("{some:json}");
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                
client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;"));
                
HttpResponseMessage response = await client.PostAsync("https://SERVER_ADRESS", content);

I suspect the request header is the reason, and send StringContent instead of FormUrlEncodedContent

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

1 Comment

Unfortunately this didn't work, I managed to solve it by simply running a Python script containing the provided template. Thanks for your efforts though!

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.