0
$\begingroup$

everyone!

I've been using Mathematica to progress through the problems of codeabbey.com, and I've picked up the first of "interactive" problems (Problem #176 - Say 100), which is sort of an introductory problem to communicating with a server via POST requests, a completely new topic to me.

I am supposed to make a POST to request to the problem ("game") server with the supplied token and I should get back a response with a random number up to 100.

So, I try with the following request

requestWithToken = 
 HTTPRequest[
   "http://codeabbey-games.atwebpages.com/say-100.php", <|
    Method -> "POST", CharacterEncoding -> "ASCII", 
    "Body" -> "token: <my_personal_token_goes_here>"|>]
requestWithToken["Body"]

and I get back a request object with a 200 status code, which means I have successfully communicated with the server, and a text response that says

error: Content-type (text/plain;charset=utf-8) do not match data
format (text/plain)!

What am I to make of this response? That the server expects utf-8 or that it expects plain text and I'm sending utf-8? Whatever I've tried at CharacterEncoding (e.g, None, "ANSI", "UTF-8") doesn't alter the response I get. So, can anyone help me make sense of the response and form a correct request?

$\endgroup$
2
  • $\begingroup$ Try "ContentType"->"text/plain". $\endgroup$ Commented Apr 17, 2022 at 1:39
  • $\begingroup$ Tried it, but it didn't work. I get the same response from the server $\endgroup$ Commented Apr 18, 2022 at 8:17

1 Answer 1

2
$\begingroup$

Well, in the rare case it helps anyone, I realised, after solving the same problem using curl, that it was a case of malformed command. What I should have written instead was

problemURL = "http://codeabbey-games.atwebpages.com/say-100.php";
sessionToken = "nU5YKQ37Fra0GyiL/uFL2Tc0";
URLRead[HTTPRequest[
   problemURL, <|"Method" -> "POST", 
    "Body" -> {"token" -> sessionToken}|>]]["Body"]

which would result in a string like "answer: x" where x a random integer between 1 and 99

and at the next step I would just have to POST again using

URLRead[HTTPRequest[problemURL, <|"Method" -> "POST", 
     "Body" -> {"token" -> sessionToken, 
       "answer" -> 100 - x}|>]]["Body"] 

so both the token and the answer are passed as arguments in the "Body" list. Nice to finally get it. Have a good one!

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.