My rails application need to send some data to a php application, which expects a POST call.
I use the folowing code:
uri = URI.parse(apiUrl)
req = Net::HTTP::Post.new(uri.to_s, initheader = {'Content-Type' =>'application/json'})
req.basic_auth(api_key, token)
req.set_form_data({"action" => action, "data" => data})
http = Net::HTTP.new(uri.host, uri.port)
response = http.request(req)
Where data is a hash converted to json:
data = {
:key1 => val1,
:key2 => val2
}.to_json
(it is a nested hash, i.e. some values are hash as well)
My problem is that the php application receives 4 backslashes before each quotation mark:
$data_json = $_POST['data'];
error_log($data_json);
and in error log I see:
'{\\\\"key1\\\\":val1,\\\\"key2\\\\":\\\\"val2\\\\"}'
Looks like rails add one of them, but even if I remove it and replace it with the following code:
a.gsub!(/\"/, '\'')
I still get many backslashes inside the php application, hence cannot convert the string to array.
Any idea??
By using set_form_data net/http is POSTing the form as urlencoded. Its NOT posting your request body as pure JSON.
If you want to POST raw JSON you will need to follow a pattern like:
uri = URI('https://myapp.com/api/v1/resource')
req = Net::HTTP::Post.new(uri, initheader = {'Content-Type' =>'application/json'})
req.body = {param1: 'some value', param2: 'some other value'}.to_json
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
Related
I have some Session values that I am constantly changing via Ajax calls. I can't seem to get a handle on the POST data to process it and set the values.
What I am passing to it here is an array of strings like is shown in my code below.
Here is where AJAX calls:
var sessionValues = [];
str = {"PID": "1", "Level": "Main", "MenuName": "Kitchen", "State": "CHECKED"}
sessionValues.push(str);
var postObj = {"sessionData": sessionValues};
$.ajax({
type: 'POST',
data: {'data': postObj},
url: 'setSession.asp'
}).done(function(response){
console.log(response);
})
I have this working fine in a PHP version of the program but my ASP version is not grabbing the data. Here is my PHP ver and the ASP ver as best as I could convert it.
<-- php setSession.php works fine -->
$data = $_POST['data'];
foreach ($data['sessionData'] as $key => $value) {
$projectProduct = "1";
$level = $value["Level"];
$menuName = $value["MenuName"];
$state = $value["State"];
$_SESSION['PID:'.$projectProduct][$level][$menuName]['menu_state'] = $state;
echo "[PID:".$projectProduct."][".$level."][".$menuName."][".$state."]<br>";
}
0 =>>>>> Array<br>[PID:1][Main][Kitchen][CHECKED]
Here I want to do the same thing in ASP
' setSession.asp
data = Request.Form("data")
For Each part In data("sessionData")
projectProduct = part("PID")
level = part("Level")
menuName = part("MenuName")
state = part("State")
Session("PID:" & projectProduct).Item(level).Item(menuName).Remove("menu_state")
Session("PID:" & projectProduct).Item(level).Item(menuName).Add "menu_state", state
response.write("[PID:" & projectProduct&"]["&level&"]["&menuName&"]["&state&"]<br>")
Next
outputs blank
It looks like it never has any data but doesn't throw any errors. Am I reading the POST object correctly?
[edit]
Here is the RAW POST data captured from Fiddler:
data%5BsessionData%5D%5B0%5D%5BPID%5D=1&data%5BsessionData%5D%5B0%5D%5BLevel%5D=Main&data%5BsessionData%5D%5B0%5D%5BMenuName%5D=Kitchen&data%5BsessionData%5D%5B0%5D%5BState%5D=CHECKED
here I used a URL Decode on that string-
data[sessionData][0][PID]=1&data[sessionData][0][Level]=Main Level Plan&data[sessionData][0][MenuName]=Kitchen&data[sessionData][0][State]=CHECKED
This looks like I should be able to loop through the strings now by using
For Each part In Request.Form("data[sessionData]")
but nothing happens. I added a simple loop to look at the request.form and here is what it is seeing:
for each x in Request.Form
Response.Write(x)
Next
' outputs -> data[sessionData][0][PID]data[sessionData][0][Level]data[sessionData][0][MenuName]data[sessionData][0][State]
I guess what this comes down to is just reading through and processing that string correctly, or multiple if more than one is sent. Correct?
The RAW output definitely helps work out what is going on.
What is happening is jQuery is translating the JSON structure into HTTP POST parameters but during the process, it creates some overly complex key names.
If you break down the key value pairs you have something like
data[sessionData][0][PID]=1
data[sessionData][0][Level]=Main Level Plan
data[sessionData][0][MenuName]=Kitchen
data[sessionData][0][State]=CHECKED
As far as Classic ASP is concerned the this is just a collection of string key and value pairs and nothing more.
The correct approach to work out what these keys are is to do what you have done in the question, but with some minor alternations.
For Each x In Request.Form
Response.Write(x) & "=" & Request.Form(x) & "<br />"
Next
Which when outputted as HTML will look similar to the break down shown above.
Armed with the knowledge of what the keys are you should be able to reference them directly from the Request.Form() collection.
Dim pid: pid = Request.Form("data[sessionData][0][PID]")
Response.Write pid
Output:
1
I have a problem with encoding and decoding json data. In js I send query with data type 'json', it looks like this:
{\"front\":{\"0\":{\"type\":\"text\",\"width\":\"55px\",\"height\":\"27px\",\"top\":\"151px\",\"left\":\"86px\",\"zIndex\":\"1\",\"svg\":\"<svg width=\\\"54.9375\\\" height=\\\"27.09375\\\" viewBox=\\\"0 0 54.9375 27.09375\\\" xmlns=\\\"http://www.w3.org/2000/svg\\\" xmlns:xlink=\\\"http://www.w3.org/1999/xlink\\\"><g id=\\\"0.7882792934370437\\\"><text fill=\\\"#FF0000\\\" stroke=\\\"none\\\" stroke-width=\\\"0\\\" stroke-linecap=\\\"round\\\" stroke-linejoin=\\\"round\\\" x=\\\"\\\" y=\\\"\\\" text-anchor=\\\"start\\\" font-size=\\\"24px\\\" font-family=\\\"arial\\\" data-textcurve=\\\"1\\\" data-itemzoom=\\\"1 1\\\" data-textspacing=\\\"0\\\"><textPath xmlns:xlink=\\\"http://www.w3.org/1999/xlink\\\" xlink:href=\\\"http://138.68.62.219/Buy-Hanes-T-shirt-PC61LS#textPath-item-0\\\"><tspan dy=\\\"0\\\">Hello</tspan></textPath></text></g><defs><path id=\\\"textPath-item-0\\\" d=\\\"M 0.125 22.117808976867764 A 3093.9720937064453 3093.9720937064453 0 0 1 54.124314613414626 22.117808976867764\\\"></path></defs></svg>\",\"rotate\":0,\"text\":\"Hello\",\"color\":\"#FF0000\",\"fontFamily\":\"arial\",\"align\":\"center\",\"outlineC\":\"none\",\"outlineW\":0}}
Then I save it to cell in MySQL, it looks like this:
{\\"front\\":{\\"0\\":{\\"type\\":\\"text\\",\\"width\\":\\"55px\\",\\"height\\":\\"27px\\",\\"top\\":\\"151px\\",\\"left\\":\\"86px\\",\\"zIndex\\":\\"1\\",\\"svg\\":\\"<svg width=\\\\\\"54.9375\\\\\\" height=\\\\\\"27.09375\\\\\\" viewBox=\\\\\\"0 0 54.9375 27.09375\\\\\\" xmlns=\\\\\\"http:\/\/www.w3.org\/2000\/svg\\\\\\" xmlns:xlink=\\\\\\"http:\/\/www.w3.org\/1999\/xlink\\\\\\"><g id=\\\\\\"0.7882792934370437\\\\\\"><text fill=\\\\\\"#FF0000\\\\\\" stroke=\\\\\\"none\\\\\\" stroke-width=\\\\\\"0\\\\\\" stroke-linecap=\\\\\\"round\\\\\\" stroke-linejoin=\\\\\\"round\\\\\\" x=\\\\\\"\\\\\\" y=\\\\\\"\\\\\\" text-anchor=\\\\\\"start\\\\\\" font-size=\\\\\\"24px\\\\\\" font-family=\\\\\\"arial\\\\\\" data-textcurve=\\\\\\"1\\\\\\" data-itemzoom=\\\\\\"1 1\\\\\\" data-textspacing=\\\\\\"0\\\\\\"><textPath xmlns:xlink=\\\\\\"http:\/\/www.w3.org\/1999\/xlink\\\\\\" xlink:href=\\\\\\"http:\/\/138.68.62.219\/Buy-Hanes-T-shirt-PC61LS#textPath-item-0\\\\\\"><tspan dy=\\\\\\"0\\\\\\">Hello<\/tspan><\/textPath><\/text><\/g><defs><path id=\\\\\\"textPath-item-0\\\\\\" d=\\\\\\"M 0.125 22.117808976867764 A 3093.9720937064453 3093.9720937064453 0 0 1 54.124314613414626 22.117808976867764\\\\\\"><\/path><\/defs><\/svg>\\",\\"rotate\\":0,\\"text\\":\\"Hello\\",\\"color\\":\\"#FF0000\\",\\"fontFamily\\":\\"arial\\",\\"align\\":\\"center\\",\\"outlineC\\":\\"none\\",\\"outlineW\\":0}}
When I read with php this data that's what I see ( $tmp=$products[0]['design_file']; print_r($tmp);) :
"{\\"front\\":{\\"0\\":{\\"type\\":\\"text\\",\\"width\\":\\"55px\\",\\"height\\":\\"27px\\",\\"top\\":\\"151px\\",\\"left\\":\\"86px\\",\\"zIndex\\":\\"1\\",\\"svg\\":\\"<svg width=\\\\\\"54.9375\\\\\\" height=\\\\\\"27.09375\\\\\\" viewBox=\\\\\\"0 0 54.9375 27.09375\\\\\\" xmlns=\\\\\\"http:\/\/www.w3.org\/2000\/svg\\\\\\" xmlns:xlink=\\\\\\"http:\/\/www.w3.org\/1999\/xlink\\\\\\"><g id=\\\\\\"0.7882792934370437\\\\\\"><text fill=\\\\\\"#FF0000\\\\\\" stroke=\\\\\\"none\\\\\\" stroke-width=\\\\\\"0\\\\\\" stroke-linecap=\\\\\\"round\\\\\\" stroke-linejoin=\\\\\\"round\\\\\\" x=\\\\\\"\\\\\\" y=\\\\\\"\\\\\\" text-anchor=\\\\\\"start\\\\\\" font-size=\\\\\\"24px\\\\\\" font-family=\\\\\\"arial\\\\\\" data-textcurve=\\\\\\"1\\\\\\" data-itemzoom=\\\\\\"1 1\\\\\\" data-textspacing=\\\\\\"0\\\\\\"><textPath xmlns:xlink=\\\\\\"http:\/\/www.w3.org\/1999\/xlink\\\\\\" xlink:href=\\\\\\"http:\/\/138.68.62.219\/Buy-Hanes-T-shirt-PC61LS#textPath-item-0\\\\\\"><tspan dy=\\\\\\"0\\\\\\">Hello<\/tspan><\/textPath><\/text><\/g><defs><path id=\\\\\\"textPath-item-0\\\\\\" d=\\\\\\"M 0.125 22.117808976867764 A 3093.9720937064453 3093.9720937064453 0 0 1 54.124314613414626 22.117808976867764\\\\\\"><\/path><\/defs><\/svg>\\",\\"rotate\\":0,\\"text\\":\\"Hello\\",\\"color\\":\\"#FF0000\\",\\"fontFamily\\":\\"arial\\",\\"align\\":\\"center\\",\\"outlineC\\":\\"none\\",\\"outlineW\\":0}}
If I do this:
$tmp=$products[0]['design_file'];
$info=json_decode($tmp);
print_r($info);
It gives me Null.
How can I get my array back from that mess?
Javascript sending:
jQuery(document).triggerHandler( "before.addtocart.design", datas);
var finaldata=JSON.stringify(datas);
jQuery.ajax({
url: 'index.php?route=checkout/cart/add', //URL TO CONTROL FUNCTION add()
type: 'post',
data: 'product_id=' + product_id + '&quantity=' + q+"&option['options']="+finaldata,
Read in Php
if (isset($this->request->post['option'])) {
$option = array_filter($this->request->post['option']);
Pass to view
$data['products'][] = array(
'design_file'=>$product['design_file'],
...
$tmp=$products[0]['design_file'];
You might be storing malformed JSON data. Take a look at these pages (some useful resources right from PHP docs):
http://php.net/manual/en/function.json-encode.php
http://php.net/manual/en/json.constants.php
PS.: The second link is related to the options parameter used in json_encode. You might compose those values the way you want, just like the example below:
$obj = [
"prop1" => 123,
"prop2" => [
"prop2.1" => "Value A",
"prop2.2" => [1, 2, 3, 4, 5]
]
];
$jsonContent = json_encode($obj, JSON_PRETTY_PRINT | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
By the way, you still can send your JavaScript objects to the server using a contentType: 'application/json' setting in $.ajax function call. That will enable you to handle request data from that HTTP request as a PHP object, as it really should be.
Use PHP to serialize your JSON content in order to store it in database instead of bypassing the string directly from the request attributes.
Something is really messed up here. First, your initial JSON is not valid. Somehow it is already has extra slashes.
Check here: Issue with JSON.stringify adding a extra \ and "" to my Json object.
Maybe here: JSON.stringify escaping without need
Also there is missing } in the very end of JSON string (it should be }}} three closing brackets).
Secondly, I guess before you inserted it in database you escaped it (so thats how you got even more slashes) and you did htmlspecialchars(). Considering this, thats how you can parse it in object.
$info = json_decode(stripslashes(htmlspecialchars_decode($tmp.'}')));
I have some PHP code which queries a MySQL database for a count.
When queried via a browser I get the following output:
{"count":"123"}
I also have a Ruby script which executes the same PHP script via Net::HTTP but the output is different:
{"count"=>"123"}
Why is this?
//The URL
uri = URI.parse("http://lab/count.php")
http = Net::HTTP.new(uri.host, uri.port)
//Request URL
request = Net::HTTP::Get.new(uri.request_uri)
//Basic authentication
request.basic_auth("user1", "secret")
response = http.request(request)
//Response
response = JSON.parse(response.body)
puts results
//Value 'count'
count = JSON.parse(response.body)[0]
puts count
Thanks.
{"count"=>"123"} is not JSON response.
It's ruby literal for Hash table.
I think you are seeing the result of parsed JSON:
>> require 'json'
>> JSON.parse('{"count":"123"}') # => {"count"=>"123"}
>> puts JSON.dump({"count"=>"123"}) # prints => {"count":"123"}
UPDATE response to comment
To get 123 printed.
uri = URI.parse("http://lab/count.php")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth("user1", "secret")
response = http.request(request)
response = JSON.parse(response.body)
puts response['count']
I am using AFJSONRequestOperation to request a server and parse the returned JSON response, but while parsing, I got this error:
NSDebugDescription = "JSON text did not start with array or object and option to allow fragments not set.";
I checked the API and it's returning JSON data:
header('Content-type: text/json');
$arr[] = array("Message" => "update succeeded");
echo '{"Result":'.json_encode($arr).'}';
Any idea how to fix that?
EDIT
I tried to call the API from browser and include the request in the url, and so I got a valid JSON response:
{"Result":[{"Message":"update succeeded"}]}
First thing, json_encode the entire object rather than breaking into it pieces.
Secondly, unless $arr contains multiple elements (not clear from example above), it should be initalized as thus:
$arr = array("Message" => "update succeeded");
I'm still not certain what else could be the issue here. You should echo out what your app is receiving and that should indicate the issue.
Please use acceptable content type.
in your webservice that should be only plain text.
here is my swift code and fixed:
let manager = AFHTTPRequestOperationManager()
manager.requestSerializer=AFJSONRequestSerializer()
manager.responseSerializer = AFHTTPResponseSerializer();
manager.GET(
baseURL + (webServiceType as String) + secureParam,
parameters:[:],
success:
{ (operation: AFHTTPRequestOperation!,
responseObject: AnyObject!) in
completion(obj: responseObject)
},
failure:
{ (operation: AFHTTPRequestOperation!,
error: NSError!) in
completion(obj: nil)
})
Check you have added /api/ before your base url of API like
http:// someurl / yourBasrUrl /api/apiName
To make a valid json response, your code should
look something like this:
$response = array(
"Result" => array(
"Message" => "update succeeded"
)
)
echo json_encode($response);
If you need read fragment json you can use option .allowFragments like this:
JSONSerialization.jsonObject(with: someData, options: .allowFragments)
Please try this:
let manager = AFHTTPSessionManager(baseURL: NSURL("Base URL"))
manager.requestSerializer = AFJSONRequestSerializer()
manager.responseSerializer = AFJSONResponseSerializer()
let params = [
"foo": "bar"
]
manager.POST("API url", parameters: params,
success: {
(task: NSURLSessionDataTask!, responseObject: AnyObject!) in
print("success")
},
failure: {
(task: NSURLSessionDataTask!, error: NSError!) in
print("error")
})
First of all, your API is returning improper Content-Type. Proper content type for JSON data is application/json. This may be conflicting while using third-party libraries.
Secondary, you should not produce json string "by hand". Altogether API should be modified to face:
header('Content-type: application/json');
$arr[] = array("Message" => "update succeeded");
echo json_encode(array('Result' => $arr));
Last bat not least. There is one more thing in charge possible: you might have BOM characters at very beginning of your api PHP script. Those are whitespace, so you may not see them in browser. Please, ensure that your PHP files are encoded without BOM.
I am stuck a long time with trying to send a JSON from javascript to a PHP script : the sending is fine (I can see the JSON in fiddler) yet I receive nothing in the PHP script :
javascript:
var person = {
name: 'yoel',
age: 28
};
xmlhttp.open("POST","http://localhost:8888/statisticsdb.php",true);
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.send(JSON.stringify(person));
php :
echo 'trying to print ' . var_dump($_POST["name"]);
I would expect obviously to see SOMETHING but var_dump returns nothing. Help would be much appreciated!
try:
$data = json_decode(file_get_contents('php://input'));
var_dump($data->name);
the reason for this is, that the body of your POST-request is:
{"name":"yoel","age":28}
though, php expects something like (ref):
name=yoel&age=28
The json string can not be parsed properly, and thus $_POST will be empty.
$_POST holds value decoded from request having Content-Type application/x-www-form-urlencoded, i.e. it parses:
param1=value1¶m2=value2
into:
array( 'param1' => 'value1', 'param2' => 'value2')
If you send data in json format, you have to json_decode it from the raw php input:
$input = file_get_contents('php://input');
$jsonData = json_decode($input);
And you'll have a PHP object filled with your json stuff.
Add this:
xmlhttp.setRequestHeader("Content-length", JSON.stringify(person).length);