C# - PostAsync response returning empty array

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



C# - PostAsync response returning empty array



I am writing a mobile application in Xamarin.Forms, and have a very simple PHP file for my back end. The mobile application sends a post request to the PHP file, and the PHP file is meant to var_dump the post contents.


var_dump


<?php
echo "Your post response is...";
var_dump($_POST);
?>



My Xamarin application uses the HttpClient class to create a simple post request using the PostAsync() method.


HttpClient


PostAsync()


public class RestService

HttpClient client;
private const string URL = "https://braz.io/mobile.php";

public RestService()

client = new HttpClient();
client.MaxResponseContentBufferSize = 256000;


public async Task<string> getUserInformation()

User u = new User("Barns", "password1234", "email@email.com");
string json = JsonConvert.SerializeObject(u);
var uri = new Uri(string.Format(URL, string.Empty));
try

client.DefaultRequestHeaders.Add("Accept", "application/json");
StringContent s = new StringContent(json);
var response = await client.PostAsync(uri, s);
string body = await response.Content.ReadAsStringAsync();

catch (Exception ex)
Console.WriteLine(ex);

return null;




For some reason, my post request is only getting the response Your post resonse is... followed by an empty array. It is strange, because when I use PostMan on google chrome, it returns the correct information.


Your post resonse is...


PostMan



I have checked that my json variable has valid JSON in it as well, so I am unsure why the PostAsync function is returning/sending an empty array from/to my PHP file.


json


PostAsync



UPDATE as per comment request



The JSON I am sending is as follows:



""username":"Barney","password":"1234","email":"email@email.com""


""username":"Barney","password":"1234","email":"email@email.com""



My user class is:


user


public class User

public User()

public string username get; set;
public string password get; set;
public string email get; set;

public User(string username, string password, string email)


this.username = username;
this.password = password;
this.email = email;






@mjwills yes it does work from postman, I will update my question
– Barney Chambers
Aug 6 at 2:30





Possible duplicate of How to get request content (body) in PHP?
– Cheesebaron
Aug 6 at 7:44





It doesn't matter, you are reading stream content. Whether it is xml, json, protobuf or something else, it is the same
– Cheesebaron
Aug 6 at 8:29




1 Answer
1



The issue has nothing to do with the Xamarin side of things. Your issue lies in the PHP code you provided and the fact that when you tried sending something with POSTman, you didn't send it as the body of the request.



In order to read the request body, you need to read the input stream:


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



Then you can output that with


var_dump($json);



As for your RestService code, I would advise you to provide the content type for your string content:


var s = new StringContent(json, Encoding.UTF8, "application/json");






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS