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.
Related
By default, Servant returns plain string requests even if the requested endpoint returns JSON
$ http $S/signup email=mail#domain.com
HTTP/1.1 400 Bad Request
Connection: keep-alive
Date: Tue, 14 Apr 2020 15:59:32 GMT
Server: nginx/1.17.9 (Ubuntu)
Transfer-Encoding: chunked
Error in $: parsing Credentials.Credentials(Credentials) failed, key "password" not found
I am trying to wrap such strings into simple JSON dictionaries:
$ http $S/signup email=mail#domain.com
HTTP/1.1 400 Bad Request
Connection: keep-alive
Date: Tue, 14 Apr 2020 15:59:32 GMT
Server: nginx/1.17.9 (Ubuntu)
Transfer-Encoding: chunked
{"error": "Error in $: parsing Credentials.Credentials(Credentials) failed, key \"password\" not found"}
But it looks like it's not that easy.
This question states possible solutions but I can't make them work today Custom JSON errors for Servant-server
Another approach is discussed in this thread https://github.com/haskell-servant/servant/issues/732 but it looks like overkill to such a simple task.
I wonder if there is a simple and robust solution in 2020?
There is a library called servant-errors. It provides a middleware that does exactly what you are looking for – transforms error responses to have a uniform structure of your choice, JSON being one of the built-in options.
See the documentation for details, but the basic usage is as straightforward as wrapping
errorMw #JSON #["error", "status"]
around your appilcation.
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.
folks!
I'm looking for the best and efficient way to parse server responds that content both HTML and XML stuff. The respond come from servers I need to poll each 5 minutes (it's about half a thousand of them in list currently, but it will double very soon). Respond stored in buffer as plane text (got from socket). So, I need to parse HTML part and in case of success (mandatory things found) I should then try to parse XML part and get statistics information to store in DB. The responses are like this:
HTTP/1.0 200 OK
Connection: close
Content-Length: 682
Content-Type: text/xml; charset=utf-8
Date: Sun, 09 Mar 2014 15:44:52 GMT
Last-Modified: Sun, 09 Mar 2014 15:44:52 GMT
Server: DrWebAV-DeskServer/REL-610-AV-6.02.0.201311040 Linux/x86_64 Lua/5.1.4 OpenSSL/1.0.0e
<?xml version="1.0" encoding="utf-8"?><avdesk-xml-api API='2.1.0' API_BUILD='20130709' branch='REL-610-AV' oper='get-server-info' rc='true' timestamp='20140309154452987' version='6.02.0.201311040'><server><id>00c1d140-d21d-b211-a828-b62919c4250d</id><platform>Linux 2.6.39-gentoo-r3 x86_64 (4 SMP Mon Oct 24 11:04:40 YEKT 2011)</platform><version>6.02.0.201311040</version><statistics from='20140301000000000' till='20140309235959999'><noviruses/><stations total='101'><online>5</online><deinstalled>21</deinstalled><blocked>0</blocked><expired>81</expired><offline>96</offline><activated>74</activated><unactivated>27</unactivated></stations></statistics></server></avdesk-xml-api>
And could be smth. like this
HTTP/1.0 401 Authorization Required
Cache-Control: post-check=0, pre-check=0
Connection: close
Content-Length: 421
Content-Type: text/html; charset=utf-8
Date: Sun, 09 Mar 2014 15:44:22 GMT
Expires: Date: Sat, 27 Nov 2004 10:18:15 GMT
Last-Modified: Date: Sat, 27 Nov 2004 10:18:15 GMT
Pragma: no-cahe
Server: DrWebAV-DeskServer/REL-610-AV-6.02.0.201311040 Linux/x86_64 Lua/5.1.4 OpenSSL/1.0.1
WWW-Authenticate: Basic realm="Dr.Web XML API area"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><TITLE>Unauthorized</TITLE><BODY><STRONG>Unauthorized</STRONG><P>The error "401 Unauthorized" occured while processing request you had sent.<P><BR><BR><I>Access denied or your browser does not support HTTP authentication!</I><BR><P><BR><BR><HR><P>Dr.Web ® AV-Desk Server REL-610-AV 6.02.0.201311040 Linux/x86_64 Lua/5.1.4 OpenSSL/1.0.1</BODY></HTML>
Concerning HTML part I'm basically interested in HTTP/1.0 STRING and Server: STRING stuff, and then need per-tag XML parsing, if authorization succeeded.
I have found, that libxml2 is suitable for parsing both HTML/XML stuff, but unable to find any real examples how to use it, just some major interface description. So, help needed.
Code examples for libxml2 are here
The mailing list is friendly, and the code is mature and good quality.
However, nothing in your example suggests you need to parse HTML. You need to parse (I think) HTTP to process the headers (and detect the 401 error from the HTTP response), then parse the XML content. Parsing HTTP headers to the level you require it is trivial (just strtok the response separating on line breaks and the first line has the answer you need). The body of the response starts after a double line break (I think your second example has a paste error). This reduces your task to simply processing HTTP headers and XML (no HTML parsing).
I'm trying to upload a file via Box API and I want to specify the content_modified_at parameter. However, I always receive an error in response, saying that I specify the date not in ISO 8601 format. I double-checked the format I'm sending, and it's definitely ISO 8601.
If I don't specify this field, the file is created successfully.
The field in my request has format "2013-04-21T19:39:16+03:00",
and I'm not sure how this is different from "2012-12-12T11:04:26-08:00" that is used in Box API docs
Here is the full listing of my request and response:
-------------- REQUEST --------------
POST https://upload.box.com/api/2.0/files/content
Accept-Encoding: gzip
Authorization: Bearer [hidden]
User-Agent: Google-HTTP-Java-Client/1.14.1-beta (gzip)
Content-Type: multipart/form-data; boundary=__END_OF_PART__
--__END_OF_PART__
Content-Type: application/octet-stream
content-disposition: form-data; name="filename"; filename="file1.txt"
1234567890
--__END_OF_PART__
content-disposition: form-data; name="parent_id"
760503410
--__END_OF_PART__
content-disposition: form-data; name="content_modified_at"
2013-04-21T19:39:16+03:00
--__END_OF_PART__--
-------------- RESPONSE --------------
HTTP/1.1 400 Bad Request
Date: Sun, 21 Apr 2013 17:01:23 GMT
Content-Length: 193
X-Error-Handling: entry;index.php|rid;132352423451741b638d902|runmode;box_api_v2|user;2|user;193761140|
Connection: close
Content-Type: application/json
Server: Apache/2.2.15 (Scientific Linux)
Cache-Control: no-cache, no-store
{"type":"error","status":400,"code":"bad_request","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"not a valid iso 8601 formatted date","request_id":"132352423451741b638d902"}
UPDATE:
Does anybody of the Box team actually care about this? Does it mean there is a mistake in the services or just nobody wants to explain me what was my fault?
There is an issue with our time stamp parser that we're in the process of fixing.
You should see now issues if you set the timestamp to UTC and send it with a
timezone offset of -00:00, e.g.
2013-04-17T09:12:36-00:00
Again, this is an issue on our end that we're working on resolving.
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.