Retrieve JSON Request Payload in WCF Service - wcf

I have an IErrorHandler set up for funneling all wcf errors through log4net. I'd like to get the json payload data from the request before logging it to the server, but I can't seen to find it in System.Web.Context.Current.Request. I expected it to be in the InputStream, but that's empty.
I'm currently using jquery to do an AJAX post with the json passed in as data.
$.ajax({
url: 'http://test.com/myservice/service.svc',
data: JSON.stringifyWcf({"id":1, "description":"thing"}),
type: 'POST',
processData: true,
cache: false,
contentType: 'application/json; charset=utf-8',
timeout: 5000,
dataType: 'json',
success: function (result) {
//do stuff
}
});
Where I would like to get the payload {"id":1, "description":"thing"}

I guess that the input stream is empty because it has already been consumed. You'll need to hook into the system before it reads the input stream to save it for later I believe. See Request.InputStream is empty when service call is made

How about: OperationContext.Current.RequestContext.RequestMessage?

Related

Bandcamp api: What POST info do you send, when querying the my_bands endpoint?

https://bandcamp.com/developer/account#my_bands
It doesnt say what youre sposta send, as POST, and if you send without a POST or empty array, you get a 'must be POST' error. Their support isnt helping.
I have been able to use their other endpoints, so I know I've got the auth correct.
I send an empty payload to https://bandcamp.com/api/account/1/my_bands, like:
var parameters = {
headers: { Authorization: 'Bearer '+access_token },
method: 'post',
payload: '',
muteHttpExceptions: true // we want to handle exceptions gracefully
};

Server response "unsupported media type" when I send JSON format data to Asp.net Core 3.0

Visual Studio 2019
Asp.Net Core 3.0
A. Unsupported media type code
$.ajax({
url: this.url,
data: jsonDataParameter,
cache: false,
type: "Post",
dataType: 'JSON',
contentType: "application/json",
success: function (data) {}
});
B. Successful request code
$.ajax({
url: this.url,
data: JSON.stringify(jsonDataParameter),
cache: false,
type: "Post",
dataType: 'JSON',
contentType: "application/json",
success: function (data) {}
});
Here is my questions:
Is this features or Bugs?
If it is features, why?
Thank you ahead.
contentType is the type of data you're sending, and application/json; charset=utf-8 is a common one to send json data.
In your case, data {a:1,b:2} is only a Javascript object so you need to use the JSON.stringify() method to convert a JavaScript object or value to a JSON string.
Since your content type is application/json;, you need to use [FromBody] and receive the data as an object based on your situation.

Crossdomain jQuery ajax request was not called and give error status "parsererror"

I am trying to call cross domain url. which has response text as below.and it is valid json response.
[{"LANG_CODE":"UK_EN","COU_ISO_CODE":"BGR"},
{"LANG_CODE":"UK_EN","COU_ISO_CODE":"HUN"},
{"LANG_CODE":"UK_EN","COU_ISO_CODE":"PRT"},
{"LANG_CODE":"UK_EN","COU_ISO_CODE":"UGA"}]
Jquery ajax code which i am using for calling cross.
$.ajax({
url: "http://someDomainName/restfulservice/Api/Countries/Get_Json",
dataType: 'jsonp',
crossDomain: true,
async: false,
success: function (data) {
alert("success >> "+data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert("error : "+errorThrown + ", textStatus >> "+textStatus);
}
});
every time it goes to error block. when i inspect this service in browser then it gives response text with valid json string.but through code i am getting error "jQuery18305917718204907095_1409810925309 was not called, status: parsererror".
while this code is working for the url "http://api.geonames.org/findNearbyPlaceNameJSON?lat=47.3&lng=9&username=demo".
what could be the issue for same ?
It is sending back JSON but you have said dataType: 'jsonp'. JSON is not JSONP.
Either change the service to support JSONP or change the client to expect JSON (which might require you to find some other way to circumvent the same origin policy)

Posting JSON object through WinJS.XHR

I'm having abit of problems posting data from WinJS.xhr to a PHP script.
"obj" is a stringified JSON object
WinJS.xhr({
type: "POST",
url: dataUrl,
headers: { "Content-type": "application/x-www-form-urlencoded" },
data: obj,
})
However the $_POST variable is always empty.
I've tried changing content-types, and escaping the object but no luck :(
Your content-type when posting json should typically be application/json
Secondly make sure you 'stringify' your json object.
Based on: Post JSON data to web services in Windows 8
WinJS.xhr({
type: "post",
url: dataUrl,
headers: { "Content-type": "application/json" },
data: JSON.stringify(obj)
})
Figured out a soloution.
Incase anyone has the same problem i got it working by removing headers from the xhr, and getting the post data # server side with this code:
$data = file_get_contents('php://input');
$data = (array) json_decode($data);

XMLHTTPRequest not working on browsers except IE

I am trying to get some data using the WCF Rest based service.
This is my code.
jQuery.support.cors = true;
$.ajax({
url: http://localhost:2545/Service/GetData,
data: JSON.stringify(temp),
beforeSend: function (xhr) { xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); },
type: "POST",
contentType: "application/json charset=utf-8",
dataType: "Json",
crossdomain: true,
success: function (result) { ProximitySucceeded(result) },
error: function (result) { debugger; ServiceFailed(result) }
});
My website is running on http://localhost:1600 and service is on http://localhost:2545.
Its working on IE fine. But on chrome/ firefox /safari returning this error
"Origin http://localhost:1600 is not allowed by Access-Control-Allow-Origin."
Please help as the service is not being accessed from any browser other than IE.
Thanks.
Mohit.
i think this may be counting as cross site scripting and being denied because the different port makes it a different domain.
to get round this you may need to setup a proxy.
hope that helps