Reputation: 45
Dictionary<string, string> data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
if (data["ReturnValue"].Equals("0"))
{
List<M_Ninushi> m_ninushis = new M_NINUSHI_DAO().GetList(data["LastUpdateDate"]);
string data_m_ninushi = JsonConvert.SerializeObject(m_ninushis);
string sentResponse = Util.FilterData(data_m_ninushi);
Dictionary<string, string> dataResponse = JsonConvert.DeserializeObject<Dictionary<string, string>>(sentResponse);
if (dataResponse["ReturnValue"].Equals("0"))
{
return 0;
}
else
{
return 1;
}
}
this id my code in webservice use asp.net. I use HttpWebRequest send data to symfony2 api FilterData
XElement xdoc = XElement.Load(configFileName);
var objStringConnection = xdoc.Descendants("URL").Select(e => new { filter_data = e.Descendants("URL_FILTER_DATA").FirstOrDefault().Value }).SingleOrDefault();
string urlAddress = objStringConnection.filter_data;
System.Net.ServicePointManager.ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => { return true; };
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
Dictionary<string, string> json = new Dictionary<string, string>();
json.Add("M_Ninushi", data);
byte[] dataSent = Encoding.ASCII.GetBytes(json.ToString());
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
//application/x-www-form-urlencoded
request.ContentLength = dataSent.Length;
Stream writer = request.GetRequestStream();
writer.Write(dataSent, 0, dataSent.Length);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
readStream = new StreamReader(receiveStream);
else
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
string dataResponse = readStream.ReadToEnd();
response.Close();
readStream.Close();
return dataResponse;
}
this id my code in webservice use asp.net. I use HttpWebRequest send data to symfony2 api I know how to send data but I don't know how to get data in symfony2 . someone help me
Upvotes: 0
Views: 343
Reputation: 95424
First, we need to correct the Content-Type
sent to the server hosting the Symfony2 application. The data you are sending is not in the proper format for application/x-www-form-urlencoded
. Change it to application/json
.
Also, JSON data MUST be encoded in Unicode. In PHP, json_decode()
only supports UTF-8 encoded strings. Therefore you must use Encoding.UTF8.GetBytes
instead of Encoding.ASCII.GetBytes
.
Dictionary.toString()
does not return a JSON string. Use Json.NET
.
In your Controller
, you can use Symfony\Component\HttpFoundation\Request::getContent()
to retrieve the form content.
<?php
namespace Company\CodeExampleBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Exception\HttpException;
class RestAPIController extends Controller
{
public function doSomethingInterestingAction(Request $request)
{
if($request->headers->get('Content-Type') !== 'application/json') {
throw $this->createBadRequestException();
}
$jsonData = json_decode($request->getContent(), true);
if($jsonData === null) {
throw $this->createBadRequestException();
}
// DO SOMETHING WITH $jsonData
}
protected function createBadRequestException()
{
return new HttpException(400, 'This endpoint expects JSON data in the POST body and requires Content-Type to be set to application/json.');
}
}
Upvotes: 1