0

I'm trying to send a JSON to my rest-api using RestSharp. Essentially I've created a model class for the json:

public class LogPostData
{
    public string LogMessage { get; set; }
    public string LogStackTrace { get; set; }
    public string LogUserId { get; set; }
    public string LogUserIp { get; set; }
}

so I perform the request in this way:

 var logPost = new LogPostData();
     logPost.LogMessage = "log message"
     logPost.LogStackTrace = "some content";

 var post = JsonConvert.SerializeObject(logPost);

 var client = new RestClient("url of rest api");

 var request = new RestRequest("methodApi", Method.PUT);
     request.RequestFormat = DataFormat.Json;
     request.AddParameter("application/json; charset=utf-8", post, ParameterType.RequestBody);
     request.RequestFormat = DataFormat.Json;

 var response = client.Execute(request);

as you can see I've created the object LogPostData and then serialized it using JsonConvert.SerializeObject.

I called the methodApi passing as parameter the json.

Now, inside my rest api, I did the following:

file_put_contents('debug.txt', serialize($_POST));

the content should be the variable that I sended with RestSharp on post variable, instead I get: a:0:{}

why my $_POST variable is empty?

pivutali
  • 161
  • 1
  • 12
  • [PHP “php://input” vs $_POST](https://stackoverflow.com/questions/8893574/php-php-input-vs-post) – FirstOne Nov 25 '17 at 15:38
  • @FirstOne so is not possible valorize $_POST using restsharp? – pivutali Nov 25 '17 at 15:44
  • I don't know C#, let alone restsharp, so I'm not capable of answering that. Can't you change the PHP portion of the project? – FirstOne Nov 25 '17 at 15:48
  • @FirstOne if I change with `php//input` I get the content sended by php, but this should appear inside $_POST – pivutali Nov 25 '17 at 15:49
  • @FirstOne you're right, but I remember when I using js in past and executed ajax.Post, the content will fill the php $_POST variable – pivutali Nov 25 '17 at 15:59
  • Do you ever actually send the request to the server using `client.Execute(request)`? I don't see that anywhere in your code. – Brian Rogers Nov 25 '17 at 16:46
  • @BrianRogers forgot to add in the example but yes I execute this, example updated anyway – pivutali Nov 25 '17 at 16:48
  • It’s because you’re doing a PUT request that $_POST is not populated. You could parse your own $_PUT array without messing up $_POST [see this answer](https://stackoverflow.com/a/41959141). Or you could use POST instead of PUT – James Nov 25 '17 at 17:00
  • @James I tried to change the method with POST but the variable `$_POST` is even empty.. – pivutali Nov 25 '17 at 17:03
  • It looks like you might be sending the request body as json. $ – James Nov 25 '17 at 17:05
  • @James what do you mean? could you please write an example^? – pivutali Nov 25 '17 at 17:07

2 Answers2

3

According to the PHP manual, $_POST works with application/x-www-form-urlencoded and multipart/form-data content types. You are sending JSON (application/json). Since $_POST is an associative array created from posted form data, and you are not posting a form, it's not surprising that it would be empty.

To get the raw JSON from the request body you need to use php://input

$json = file_get_contents('php://input');

To deserialize the JSON to an object you can use json_decode.

$logPostData = json_decode($json);

If you want the data to be converted into an associative array like $_POST, you can pass true as the second parameter:

$logPostData = json_decode($json, true);
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • ok this seems to work just a question: why if I change the method of request to POST: `Method.POST);` I still get `$_POST` empty? – pivutali Nov 25 '17 at 17:31
  • Because you are sending `application/json` not `application/x-www-form-urlencoded` – Brian Rogers Nov 25 '17 at 17:32
  • I change in the php script the header and setted as: `header('Content-Type: application/x-www-form-urlencoded');` I also change the request in c# as `new RestRequest("methodApi", Method.POST);` then I added as parameter this: ` request.AddParameter("application/x-www-form-urlencoded", post, ParameterType.RequestBody);` so I checked the content of `$_POST` with this: `file_put_contents('debug.txt', serialize($_POST)); ` and I get this : `a:0:{}` what is wrong now? – pivutali Nov 25 '17 at 17:37
  • If you change the content type to `application/x-www-form-urlencoded`, you have to actually send that type of content, i.e. url-encoded key-value pairs. You are sending JSON. They are different types of content. See [Differences in application/json and application/x-www-form-urlencoded](https://stackoverflow.com/q/9870523/10263) – Brian Rogers Nov 25 '17 at 17:50
  • I got it, as I said I changed to `application/json` and when I do this: `$rawData = file_get_contents("php://input"); $json = json_decode(rawData, true); file_put_contents('debug.txt', $rawData); ` I get nothing inside the file – pivutali Nov 25 '17 at 17:58
0

Here's what you are sending (JSON):

header: encoding-type=application/json
body: {"param1":"I like horses", "param2":"they are cool"}

This is what PHP needs in a POST request for the $_POST array to work (form submissions)

header: encoding-type=application/x-www-form-urlencoded
body: param1=I%20like%20horses&param2=they%20are%20cool

If you are happy with sending json, you need to receive it properly. If you want to keep using PUT, then make a handler for it that expects JSON. (Based on this answer)

$method = $_SERVER['REQUEST_METHOD'];
if ('PUT' === $method) {
    $data = json_decode(file_get_contents('php://input'), true);
    var_dump($data); //$data contains put fields 
}
James
  • 20,957
  • 5
  • 26
  • 41