Black
Black

Reputation: 5367

json from C# client to PHP server - empty content

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?)

Upvotes: 0

Views: 63

Answers (2)

Black
Black

Reputation: 5367

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.

Upvotes: 0

CodeFuller
CodeFuller

Reputation: 31312

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\"}";

Upvotes: 1

Related Questions