ServiceStack PUT validation - json

I'm trying to get validation to work on PUT operations using ServiceStack,
this is my client code
var client = new JsonServiceClient();
return client.Put<string>(url, content);
I have validation wired up and working on the server, since I can see the breakpoint hit and validation failing, but all I get back on the client is
HTTP/1.1 400 NotEmpty
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
Date: Sun, 06 May 2012 17:08:44 GMT
Content-Length: 0
same happens when I do a successful PUT, I just get plain string back, something is leading me to believe that ServiceStack doesn't want to return json on PUT.

Related

JSON HTTP response body from rails seems to be invalid

I am working with rails, and returning a json response with the below method
def return_json
render json: params
end
When i am viewing the response on chrome developers tools, everything seems to be right. But when i trace the HTTP response on wireshark, on HTTP response body it seems that some extra characters exists.
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Wed, 05 Jul 2017 17:07:48 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: max-age=0, private, must-revalidate
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Runtime: 0.433854
X-Request-Id: f46a0e87-6969-4285-9b80-da0223edac01
X-Powered-By: Phusion Passenger 5.1.5
Status: 200 OK
49
{"device_attributes":[{"id":"85","value":"35"},{"id":"80","value":"65"}]}
0
(extra empty line)
I'm talking about the number 49 which is in hex and it seems to be the length of the JSON string. And after that it follows a 0 with an empty line.
Wireshark screenshot which shows the response
First of all, i would like to ask, what a valid HTTP response look like?
I think that after headers, follows an empty line and then the response body and after that nothing.
And second why rails do that and if there is a way to change that. I think that rails do that, because i get the same response from apache + phusion passenger and also puma. Also i tried this from some other code, not related to rails, and the HTTP response it was as i explained earlier and not as rails does.
I did not found out the answer i was looking for, but a workaround in order for the extra info to be removed, is to render as follows:
render plain: ActiveSupport::JSON.encode({ result: :ok })
This work around does set content-type as 'text/plain' and not 'application/json'. If you set content-type in render options as 'application/json' this extra info are being displayed again.
So i will assume that has something to do with the json renderer module, but i can't research it more at this time.

Testing REST/JSON input media, IBM i Integrated Web Services (IWS) receiving err "HTTP/1.1 400 Bad Request "

I am getting error using IBM's Integrated Web Services (IWS) on the IBM i (a.k.a AS400) w/RPG and getting this message, when trying to test a RESTful web service to accept JSON as input media. When input media is ALL it works, but trying to use POST with input media JSON. BTW - I am new to all of this web stuff. Any help would be greatly appreciated.
HTTP/1.1 400 Bad Request
Date: Mon, 01 May 2017 14:19:41 GMT
Server: Apache
X-Powered-By: IBM i
Content-Length: 119
Connection: close
Content-Type: text/plain
com.ibm.as400.access.ExtendedIllegalArgumentException: javaValue ({ "pinname": "Cecil Jones" }
): Length is not valid.
When input media is ALL it works, because Content-Type: text/plain is accepted.
If you want deploy service as json service you should in request headers specify:
Content-Type : application/json

How to change the Content-Type response in the REST DataSnap

I have a project made from WebBroker REST, running as a service, I need to change theCotent-Type response to application/json and by default WebBroker REST brings me as a result the
Content-Type 'text/html; charset=ISO-8859-1'
I change the response by accessing the method
GetInvocationMetadata(True).ResponseContentType = 'application/json'
of the Data.DBXPlatform class, but it still does not resolve it it adds anotherContent-Type just below the current one.
This only happens only in WebBroker REST, if I create a project by DataSnap Server I usually get it. But I need WebBroker REST to access the information the client sends me.
Example of how to make the problem occur.
Create a Project from the DataSnap REST Application
In the ServerMethods class, do the same model.
uses System.StrUtils, Data.DBXPlatform;
function TServerMethods1.EchoString(Value: string): string;
begin
Result := Value;
GetInvocationMetadata.ResponseContentType := 'application/json';
end;
Example response Header.
Connection: close
Content-Type: text/html; charset=ISO-8859-1
Content-Length: 25
Date: Tue, 10 Sep 2013 16:41:37 GMT
Pragma: dssession=542354.126073.592372,dssessionexpires=1200000
Content-Type: application/json
In New DataSnap WebBroker Application Wizard you have the option to create a separate Server Module. In the generated unit WebModuleUnit1.pas there is a variable WebModuleClass, which is a descendant of TWebModule. This WebModuleClass object has a public property Response of type TWebResponse, which gives you ability to set ContentType.
http://docwiki.embarcadero.com/Libraries/Berlin/en/Web.HTTPApp.TWebResponse.ContentType

Get json on http protocol by recv() system call in C?

I'm receiving invalid characters on try to receiving JSON by http protocol in C.
When I send
GET /<query> http/1.1\r\nHost:<host>\r\n\r\n
then the result displays as follow:
HTTP/1.0 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Encoding: gzip
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST
Access-Control-Allow-Credentials: false
X-Content-Type-Options: nosniff
Date: Sun, 07 Dec 2014 13:14:29 GMT
Content-Length: 11410
�
I don't know why I don't receiving JSON? However, in browser's address bar I type the same query & I received the json as well.
[UPDATE # 1]
I found it useful to work on built-in library, as the error is related to network byte compressions so I used Qt's library QNetworkManager specifically to done this job.
Please use a HTTP library or make yourself comfortable with the HTTP protocol and implement it properly.
Content-Encoding: gzip
At least this line means trouble for your simple parsing. But interestingly, based on your request the server should not sent this line if a non-compressed version is available. This means not only your code is buggy, but that the server might be buggy too (it actually assume that you understand any compressions because you did not say different).
GET / http/1.1\r\nHost:\r\n\r\n
And your request is wrong too. It must be HTTP/1.1 instead of http/1.1. And it must contain a proper host header.
Qt's QNetworkManager is best for this purpose, as it do all the intricate stuff itself & provide elegant interface to user.

Service Stack Json Response Contains Extra Characters

I'm converting a Web Api project to service stack and in json responses I'm getting an extra line of text before and after the json content. I'm using fiddler to capture the response.
Edited for brevity, here is an example:
18d
[{"id": ... }]
0
What are these lines? I can't find any configuration option that would seem to correspond to keep this from happening.
Edit
I went back and started with the basic hello service stack example, and here's what I got for a response:
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/1.0
X-Powered-By: ServiceStack/3.943 Win32NT/.NET
Date: Thu, 18 Apr 2013 15:48:49 GMT
1b
{"Result":"Hello, JRandom"}
0
I'm assuming the extra response lines are the result of the Transfer-Encoding: chunked header.