0

I'm sending a JSON message from a C# client to a PHP server.

The C# client code is as follows:

    private String baseUrl = null;
    private String clientId = null;

    private static readonly HttpClient client = new HttpClient();

    private Task<string> Send(string method, Dictionary<string, string> metadata)
    {
        ServicePointManager.ServerCertificateValidationCallback = new
        RemoteCertificateValidationCallback
        (
           delegate { return true; }
        );

        String jsonRequest = "{\"method\":\"checkLicence\"}";

        StringContent content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");

        client.DefaultRequestHeaders.Add("CLIENT_ID", clientId);
        String clientSecret = encodeClientSecret(clientId);
        client.DefaultRequestHeaders.Add("CLIENT_SECRET", clientSecret);

        var result = client.PostAsync(baseUrl + "/api", content);
        HttpResponseMessage response = result.Result;
        return response.Content.ReadAsStringAsync();
    }

On the server-side, the PHP/Symfony endpoint is as follows:

/**
 * @Route("/api", name="api")
 */
public function api(Request $request) {

    $this->checkAuth();

    $params = array();
    $content = $request->getContent();
}

by PHP debugging, I find that the $content variable is empty when I receive this request. Why does it not contain the JSON string?

EDIT: to shed a bit more light on this - using Wireshark I can observe it seems my PHP server accepts the original POST request, and responds with a 302 (moved) response, and then somehow the redirect (now GET) request arrives at my /api endpoint, and has no content (because it's a redirect?)

4
  • what are the properties of your Request ? Commented Feb 12, 2018 at 2:56
  • sorry, could you be more specific? Commented Feb 12, 2018 at 4:04
  • is Request in your PHP code is a user-defined class? Commented Feb 12, 2018 at 5:18
  • @JericCruz Request refers to the Symfony class Symfony\Component\HttpFoundation\Request Commented Feb 12, 2018 at 6:31

2 Answers 2

1

You have a small typo in your JSON string. Key-values should be separated by a colon, not by an equal sign. Should be:

String jsonRequest = "{\"method\": \"checkLicence\"}";
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, but not the reason for the content arriving empty
0

this was happening because the PHP server was redirecting an HTTP request to HTTPS, and thus the POST was lost, and replaced with a GET request, which has no body.

Comments

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.