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?

11
  • PHP “php://input” vs $_POST Commented Nov 25, 2017 at 15:38
  • @FirstOne so is not possible valorize $_POST using restsharp? Commented Nov 25, 2017 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? Commented Nov 25, 2017 at 15:48
  • @FirstOne if I change with php//input I get the content sended by php, but this should appear inside $_POST Commented Nov 25, 2017 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 Commented Nov 25, 2017 at 15:59

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

5 Comments

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?
Because you are sending application/json not application/x-www-form-urlencoded
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?
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
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
1

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 
}

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.