Get HttpResult using the JsonServiceClient - servicestack

I am returning HttpResult from one of the rest service methods using servicestack's new API. Is there a way to get the HttpResult using the JsonServiceClient?
For ex: JSonServiceClient.Send<HttpResult>("DELETE","person", new { PersonID = 30 });
I want to inspect the header information from the httpresult.
Thanks.

There's no such thing as a HttpResult client response from a ServiceStack web service.
You use a HttpResult to customize the HTTP Response that's returned from your service, e.g. Add additional HTTP Headers. But the response body the client sees is still only the Response DTO (if any).
Use fiddler, wireshark, chome web inspector (if this is an Ajax call) or something like ServiceStack's Request Logger plugin to inspect the HTTP traffic.
Invalid use of ServiceStack's REST Clients
Also consider using the appropriate Clients REST API like client.Delete(), client.Get() etc instead of overloading the T.Send() method (which is usually a POST).
Use Typed DTOs in the ServiceClient instead of anonymous types which are not supported.
Inspecting HTTP Headers using the ServiceStack Service Clients
ServiceStack's Service Clients provide Local and Global WebResponse (and Request) filters that you can use to introspect the WebClient's HttpWebResponse returned for that request, e.g:
client.ResponseFilter = httpRes => {
httpRes.Headers.. //do something with returned headers
};
client.Delete(new Person { Id = 30, ...});

Related

How to run a SOAP request in NodeJs?

It might sound like a repeated question at first, but I've gone through all the blogs/ tutorials/ videos I found but none of them actually says how you run that request. Example: for a RESTful request, you code in NodeJs, hit the route(https://localhost/3000/api/getStudent) and get the response. In code you use router.post('/getStudent', async (req,res) => {
// Check response here or manipulate
})
But in soap after coding all the functions like in here-
https://medium.com/metrosystemsro/with-node-js-wrap-backend-soap-webservices-in-a-restful-api-a96887575046
https://dafabulousteach.wpcomstaging.com/2016/05/19/making-a-soap-call-with-node/
https://betterprogramming.pub/how-to-perform-soap-requests-with-node-js-4a9627070eb6
Where do I call the the function and how do I pass parameters and test it? And how do I check the response?
To place this in the context of what you are asking about, a SOAP service is just a server listening on an address for POST requests that have an XML payload. That's it.
So if your SOAP web service address is http://example.com/service_endpoint then you can call the web service by making a POST HTTP request at this address and sending it a SOAP XML message as payload.
Obviously, the XML message in the request must match something that the service expects and you know how to build that XML either by reading documentation or by using the WSDL of the SOAP web service.
So if you know how to make a POST HTTP request to a REST service address and send it a XML payload (although most commonly for REST you use JSON), you already know how to call a SOAP web service.
Now, for convenience, because SOAP is a protocol, the way you call the service and what it expects as XML payload is described by the WSDL, and you can use tools that read the WSDL and create a client API that you can invoke like any other method in your code. The tools handle the details of the HTTP POST request for you, and also the marshalling of any parameters to XML. This is probably what you found confusing.
So let's take an example.
If you have, say, a service that has an operation named savePerson which accepts firstName and lastName as parameters and is described in the WSDL, then your tooling might generate a client that has such a method and accepts a Person object and you can call it like:
var response = client.savePerson({ "firstName": "Kim", "lastName": "Seokjin" });
or some variation of this. You then get a response that you can read just like any other object. You might get a promise instead, or an event, or whatever way the client chooses for how to work. When you use this code, what happens is that the client performs a HTTP POST request behind the scenes for you and marshals the person object to XML, something like this:
POST /service_endpoint
Host: http://example.com
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<savePerson>
<person>
<firstName>Kim</firstName>
<lastName>Seokjin</lastName>
</person>
</savePerson>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
You can obviously send this request yourself with whatever HTTP library you prefer if you don't want to generate a client from the WSDL. But people prefer to use a generated client instead of dealing with these details directly (i.e. making HTTP requests, parsing XML, etc).

How to send a http response using koajs

I'm trying to validate a webhook via facebook. So facebook hits my url my-url/facebook/receive within my route in nodejs i'd do res.send(req.query['hub.challenge']); to send an http response.
I'm using KoaJS. From what i understand, Koajs merges the request and response object into ctx but when reading through the docs I can't find anything along the lines of ctx.send or similar to send a http response.
Can anyone give me some direction or links.
Thanks.
To send the body of a response, you can simply do ctx.response.body = 'Hello'. There are many aliases attached to ctx, so you don't necessarily have to reference the response or request yourself. Doing ctx.body = 'Hello' would be the same as the code above.
If you wanted to set headers, you would use the ctx.set() method. For example: ctx.set('Content-Type', 'text/plain').
To access the query parameters, you would use ctx.request.query['some-key'] (or simply the alias ctx.query['some-key']).
All of the different request/response methods are documented pretty well at the Koa website along with a list of aliases attached to ctx. I highly recommend you give it a read.

ServiceStack ServiceClient HTTP 206 and Range header

I'm using ServiceStack ServiceClient to write an API wrapper. The API returns HTTP 206 if the number of entities to be returned is too great. Is there a a good way to handle this with ServiceClient, for example for an API endpont that's wrapped like this:
var result = client.Get<IEnumerable<TResource>>("path");
Is this a ServiceStack service that's returning a HTTP 206? as this isn't behavior that's in-built into ServiceStack. If you are using a ServiceStack you may need to increase your IIS/ASP.NET Request limits to allow larger responses.
Otherwise if you're talking to a 3rd Party API, you shouldn't use ServiceStack's Service Clients which are opinionated towards consuming ServiceStack services. You should instead use something like the HTTP Utils built into ServiceStack which would allow you to specify custom HTTP Headers if your service supports it, you can ask for a Partial Content-Range by specifying it in a HTTP Request Filter, e.g:
var json = "http://example.org/users".GetJsonFromUrl(
requestFilter: httpReq => httpReq.AddRange(0, 1000));

Has anyone got servicestack work with HttpRequestMessage and HttpResponseMessage?

Has anyone had success getting servicestack to work with HttpRequestMessage and HttpResponseMessage?
What is the minimum implementation of IHttpRequest and IHttpResponse that servicestack needs in order to properly handle an http request?
IHttpRequest and IHttpResponse are just normalized lightweight interface wrappers around ASP.NET and HttpListener's server HTTP Request/Response objects. It's purpose is to provide a generic, testable API you can bind against so your services can work in both ASP.NET and HttpListener hosts. They have no connection with HttpRequestMessage / HttpResponseMessage.
Not sure what you're trying to achieve, but you can return a IHttpResult or its de-facto implementation HttpResult to customize the HTTP Headers and body output of your services response.

WCF Web API RESTful is not allowed by Access-Control-Allow-Origin

Seems like I have a cross domain access problem.
I've seen some solutions that are indicating to add "Access-Control-Allow-Origin: *", but I don't know where I can do this.
Do I need to create some handler?
I'm using WCF Web API.
Error: XMLHttpRequest cannot load http://localhost:8081/Song/0. Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin.
EDIT
I've noticed that this is only happens when HTTP method is PUT or DELETE.
I can successfully make requests with GET or POST.
I'm making the request using jquery.
$.ajax({
url: Settings.RESTfulEndPointFor('Song/' + songID),
type: 'DELETE',
success: function (response) {
callback(response);
}
});
I don't know why, but it seems like this is resulting in method OPTIONS with Access-Control-Request-Method: DELETE.
Does any one know what causing this?
Any help is appreciated.
I had this problem when connecting to a WCF RESTful service via AJAX calls
My javascript was this:
var GetData= function(){
var data;
$.ajax({
url: this.server + "/data",
async: false,
type: "GET",
success: function (success) {
data = success;
}
});
return data;
};
My service endpoint was opened with this code
ServiceHost host = new ServiceHost(new MyService());
host.Open();
All the important data is stored in the App.config file, I did not have to change that file for this fix.
I knew I had to add the headers somewhere before the response message was sent.
After some searching and hacking I found the Authorization property of a ServiceHost object. The Authorization property is an instance of the ServiceAuthorizationBehavior class whose objects have a property called ServiceAuthorizationManager which is an instance of the ServiceAuthorizationManager class.
By creating a new class that inherits from the ServiceAuthorizationManager and setting it to the ServiceAuthorizationManager property of the Authorization behavior of your ServiceHost instance, you can intercept all calls to your service.
This is how I have implemented my class
public class MyServiceAuthorizationManager : ServiceAuthorizationManager
{
protected override bool CheckAccessCore(OperationContext operationContext)
{
HttpResponseMessageProperty prop = new HttpResponseMessageProperty();
prop.Headers.Add("Access-Control-Allow-Origin", "*");
operationContext.OutgoingMessageProperties.Add(HttpResponseMessageProperty.Name, prop);
return true;
}
}
then right after I declare my ServiceHost object (before the host is opened) I add this line
host.Authorization.ServiceAuthorizationManager = new MyServiceAuthorizationManager();
After doing this, rebuilding, and running my service the error message stopped showing up. Hooray!
Lastly, I read an article that described the ServiceHost class was designed for SOAP/WSDL services not RESTful services. For RESTful services the WebServiceHost object should be used.
So
ServiceHost host = new ServiceHost(new MyService());
host.Open();
becomes
WebServiceHost host = new WebServiceHost(new MyService());
host.Open();
You must add references to the following assemblies:
System.ServiceModel.Web
Hope this helps.
Sources:
http://social.msdn.microsoft.com/Forums/en/wcf/thread/97ddb118-fdfd-4651-9e61-4d822861325f
http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/webservicehost-hosting-a-wcf-rest-service/
http://social.msdn.microsoft.com/forums/en-us/wcf/thread/551409FD-DD77-40EF-8B78-DC8B3D7EA0BA
Normally you put this in header of response. So put it in header where you modify/insert other header values like this
header('Access-Control-Allow-Origin: *) //change it according to however header is set in wcf , since this is php syntax
Point is your response should have this header.
The request you are seeing with the OPTIONS method and an Access-Control-Request-Method: DELETE header is called a "preflight request". The CORS specification requires this for requests with methods that have side effects (like DELETE) to ensure the resource is ok with the request.
Check out this section of the spec >>
http://www.w3.org/TR/cors/#cross-origin-request-with-preflight0
Unfortunately I don't know how to make this type of request work with wcf web api.
I have created
AllowCrossDomainRequestHandler : DelegatingChannel
and for each response I'm registering this header:
response.Headers.Add("Access-Control-Allow-Origin", "*");
I got this to work using the following response headers:
res.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Methods': 'DELETE, POST, GET, OPTIONS',
'Access-Control-Allow-Origin': '*'
});
First, with most web browsers there is no way to actually get around the cross-domain restriction. Most won't even let you change the "accept" header. So you have to use JSONP. JSONP is a way of getting JSON data from a cross-domain service, but it is returned in the form of a javascript snippet - which is allowed. The way it works is that you provide the callback function name to the service, then the cross-domain service returns a simple javascript with the actual JSON values embedded as the parameters to your callback function. This is really easy to do now with WCF WebApi (preview 6). Install it in VS 2010 with NuGet. Once you have it installed, look here for more information.

Resources