How to create json object and send http post in fiddleScript - fiddler

how can i create json object and send pass http post method in fiddleScript
i try but not work
var sInput = '{"a" : 1, "b" : [2, 3, 4]}';
var json = Fiddler.WebFormats.JSON.JsonDecode(sInput);

first of all, aren't you suppose to "encode" instead of "decode" before sending ?

Related

How to send a POST request to OData v4 ActionImport with SAPUI5

I would like to send a POST request to an OData v4 ActionImport. How can I achieve this in an SAPUI5 environment? I had a look at the v4 ODataModel and ODataContextBinding. There are methods for execute an ActionImport but i dont know how to set the body then.
Generally asking: How should I submit OData requests that should not necessarily be bound to the UI? For example, if I just want to query a value from the backend or send a file to the backend. Right now, I create an ODataContextBinding and call the execute/requestObject method but i think that this might not be the best approach (Also i cant set the request body this way). Maybe it might be better to make a direct ajax request?
Thanks in advance!
I stumbled on your question because I had the exact same problem. I'm providing my solution in case it helps someone else.
onValidate: function(oEvent) {
var oModel = this.getModel("reportService");
var oActivityCreateContext = this.getCreateContext();
var oActionODataContextBinding = oModel.bindContext("/validateActivity(...)");
oActionODataContextBinding.setParameter("activity", oActivityCreateContext.getObject())
oActionODataContextBinding.execute().then(
function() {
var oActionContext = oActionODataContextBinding.getBoundContext();
console.table(oActionContext.getObject().value);
}.bind(this)
);
}
The model "reportService" is a sap.ui.model.odata.v4.ODataModel. The function call is unbound and is declared this way in my service.cds file:
action validateActivity(activity : Activities) returns many rm.ValidationMessage;
The oActionContext.getObject().value contains the response to my function call.
The key here is the setParameter that sets the activity on the payload. Here's what the resulting request could look like:
POST http://localhost:8080/api/ReportService/validateActivity
Content-Type: application/json
{"activity": {
"activityNumber": 1,
"report_ID": "a3558fce-76bc-49a9-ae23-bd5566fb3bc6",
"job_code": "160",
"learningPeriod": 1,
"salaryAnnex": "D3",
"workingRegion_code": "08",
"unionName": "CSD",
"local": "Local 123",
"nbWeeksWorked": 8,
"nbHourSimple": 110,
"nbHourTimeAndHalf": 5,
"nbHourDouble": 0,
"sin": "111222333",
}}
I hope this will help others who are struggling to do this.
Regards

Send list to server without json_encode dart

Iam using http package to communicate with the server.
How to send an list to php file in the server without json_encode in Dart with Flutter.
dynamic result = null;
List ids = [12, 65, 7];
Map data = {
'name': 'Mark',
'ids': ids,
};
var client = new http.Client();
await client.post('https://example.com/control/',
headers: {
"Accept": "application/json",
},
body: data,)
.then((response) => result = jsonDecode(response.body));
My code above dont work, The problem is I need to write json_encode(ids) to send the data, and when I want to get the array/list in my php file I need to write json_decode($this->input->post('ids'))
How to solve it without json_encode and json_encode, how to send an json object which can accept arrays without any problem?
I don't understand completely what is the problem with sending json but you may convert your list to string like:
final idsSerialized = ids.map((id) => '$id').join(',');
Then send this string as payload of POST request to php script which can read value via _POST array (or your favorite way) and restore this serialized string to array:
$ids = explode($_POST['ids'], ',');

Posting multiple images to tumblr using tumblr.js api

I am trying to creating a app which can send posts to tumblr using tumblr.js api.
I could send single image using the createPhotoPost method, but I have to send multiple image in a single post via this method.
From the documentation it says that createPhotoPost method has a "data" parameter which can be an array with "URL-encoded binary contents"
When ever I try to send something in the data Array it returns "[Error: form-data: Arrays are not supported.]".
Can someone please help to solve this issue and please explain what should we pass in the array (Really I am not getting what is URL-encoded binary contents)?
Thanks in advance
There is an error in tumblr.js
https://tumblr.github.io/tumblr.js/tumblr.js.html#line504
The documentation at https://www.tumblr.com/docs/en/api/v2#posting is correct.
Basically the issue is that its not supposed to be
data = [ one , two ] its litterally params['data[0]'] = one ; params['data[1]'] = two; (?A PHP Convention)
var request = require('request')// already a tumblr.js dependency
var currentRequest = request({
url:'https://api.tumblr.com/v2/blog/someblog.tumblr.com/post',
oauth:keys,
},function(err,response,body){
currentRequest//
debugger
cb()
})
var params = {
'type' :'photo',
'caption':'Click To Verify You Are A Robot',
}
var params_images = [
fs.createReadStream('image'),
fs.createReadStream('image')
]
// Sign it with the non-data parameters
currentRequest.form(params)
currentRequest.oauth(keys);
// Clear the side effects from form(param)
delete currentRequest.headers['content-type'];
delete currentRequest.body;
// And then add the full body
var form = currentRequest.form();
//###add params_images to params keys 'data[0]' 'data[1]' 'data[2]' ....
params_images.forEach(function(image,index){
params['data['+index+']']
})
for (var key in params) {
form.append(key, params[key]);
}
// Add the form header back
var form_headers = form.getHeaders()
for(var key in form_headers){
currentRequest[key] = form_headers[key]
}

How to handle json payload in GET request Play 2.0 + Scala

I want to make a handler/controller for a GET request such as in ElasticSearch :
$ curl -XGET 'http://localhost:9200/twitter/tweet/_search?routing=kimchy' -d '{
"query": {
"filtered" : {
"query" : {
"query_string" : {
"query" : "some query string here"
}
},
"filter" : {
"term" : { "user" : "kimchy" }
}
}
}}
I read the documentation from http://www.playframework.org/documentation/2.0.4/ScalaJsonRequests but the example is based on POST request. I've tried on my own it appears that I can access to the body request with POST request. But, when I try with the GET request, my request.body is AnyContentAsEmpty.
Is there a way to handle the json from this request in Play 2.0 ?
I saw that there is no body semantic for GET : Payloads of HTTP Request Methods .
So maybe it's normal that there is no mechanism to deal with it through Play 2.0.
I believe you are confused on what can you expect on each type of request. To sum it up:
GET requests contain the payload in the URL, no request body is
added
POST requests add the payload to the request body
From the example you post it seems that you want to return a Json answer as a result from a GET request, which would make more sense.
That can be easily achieved by crafting the Json string and using the Ok(result).as("application/json") to set the MIME type of the response.
this is a sample question
Play's default body parser follows the HTTP spec, and ignores the message body for GET, DELETE, HEAD and OPTIONS methods. If you want to force it to parse a body, you can do that by explicitly passing a body parser, eg:
def delete = Action(parse.json) { implicit request =>
val json = request.body
val someProp = (json \ "someprop").as[String]
Ok(s"Prop is: $someProp")
}

How to convert request.body to a byte array in order to calculate MD5?

I'm creating a scala function in a Play! application that authenticates requests to my web-service. The authentication is basically an HMAC authentication. The method receives the type of bodyParser to be used. Since I don't know the type of the -bodyParser, how can I convert request.body to a generic Array[Byte] on which I can run my MD5 processing?
def Authenticated[T](authType : AuthenticationType, bodyParser : BodyParser[T])(f : (Request[T]) => Result) = {
Action(bodyParser) { request =>
// Authentication logic starts here
// TODO: Something like: var bodyData : Array[Byte] = request.body.toByteArray()
// The rest is easy...
var contentMD5 = calculateMD5(bodyDat)
f(request)
}
}
Any request body received by Play can be parsed to a Http.RawBuffer which can give a pure a raw byte array representation of your request body.
The line of code you're looking for is
var bodyData : Array[Byte] = request.body.asRaw.asBytes()