content-type; charset missing a space - glassfish

Using JAX-RS and Glassfish 3.1.2 I am trying to get the charset added to the content-type.
I've tried a few of the answers on SO, but I have one issue:
#Produces(
{
MediaType.APPLICATION_JSON + "; charset=utf-8",
MediaType.APPLICATION_XML + "; charset=utf-8",
})
results in a header like:
Content-Type: application/xml;charset=utf-8
However some other code (a Python library) requires it to be like:
Content-Type: application/xml; charset=utf-8
(note the space between the ; and charset).
From what I can tell the space is optional, but the Python library (out of our control) requires it.
Any ideas on what I need to do to convince JAX-RS to spit out the space?

Related

VS Code REST Client Extension: How to access nested response headers

I'm trying to access 'nested' headers with the REST Client extension for VS Code.
I defined a GET CSRF_Token request that stores the "Set-Cookie" header inside a #token_response variable.
However the header looks like this:
'csrftoken=aGHuGkRbApH3qAksHHR3uaOKG0eZsYne; expires=Wed, 22 Nov 2023 08:40:05 GMT; Max-Age=31449600; Path=/; SameSite=Lax'
I already tried to access it with something like:
#csrf_token = {{token_response.0.split(';').0.split('=').1}}
and
#csrf_token = {{token_response.//[0].//split(';')[0].//split('=')[1]}}
But this does not seem to be the correct syntax.
Also the documentation does not contain any hints on that yet, sadly.
Now my question is:
Is there a way to only extract the part of the header that contains the value of the "csrftoken" key?
And if so, how?
Here is my code:
# Get CSRF Token
# #name getToken
GET http://{{host}}/api/csrf_token
#token_response = {{getToken.response.headers.Set-Cookie}}
#csrf_token = {{token_response}}
######
# Edit existing xyz
PATCH http://{{host}}/api/xyz/1
content-type: application/json
x-csrftoken: {{csrf_token}}
origin: http://{{host}}
{
"name": "some name"
}
Thanks a lot in advance! :)

SoapUI testing: remove quotation marks from session ID

So, I am new to SoapUI, and working with API's in general, but I am trying to set up an automated test for a REST API in SoapUI (the freeware version).
The first call I make returns a session ID in JSON, which is placed between quotation markslike: "session ID goes here". This key is needed, without quotation marks, in the next API call. I use SOAPUI's "Property Transfer Teststep", and this is working well, apart from the quotationmarks which are still around the session ID, resulting in an error responce by the subsequent call.
Any idea how I can remove the quotation marks? Ive been reading up/Googling on Xpath and JSONPath, which are some of the coding languages I can use in the screen, but I cant see how to use this to remove said quotation marks.
Your help would be greatly appreciated!!
Full RAW Response:
HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/8.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Tue, 02 May 2017 18:31:48 GMT Content-Length: 38 "48d45ba2-4549-41be-8b31-e85b3c738a03"
you say that the message is returned as "48d45ba2-4549-41be-8b31-e85b3c738a03". Although the message says it is json, that does not mean that SoapUI will interprete it as valid json. Valid JSON like in the question referred to in another answer, starts and ends with curly brackets. Also it should have a key and value pair. Like so:
{
"sessionId":"48d45ba2-4549-41be-8b31-e85b3c738a03"
}
In that case the transfer $.sessionId would yield 48d45ba2-4549-41be-8b31-e85b3c738a03(without quotation marks), and not "48d45ba2-4549-41be-8b31-e85b3c738a03" (with quotation marks).
In attachment there is a project with a mock. It has got one call. If you open the project, you have to launch the mock by clicking play, then you can execute the test case. - https://www.dropbox.com/s/rsgc9q1g45jze5z/StackOverflow_43745255_QuestionQuotationMarks.xml?dl=0. You can save this as XML, then you can open it with SoapUI.
You will see a property transfer step. It has one step that is passed. Then one where you transfer the value without any paramter. It will transfer the message in full. Including the quotation marks.
The sencond transfer is '$'. For a valid json, this would render the same result as for the property transfer without any parameter because '$' represents the root. At least that is how I understand it.
Included you will also find a script:
response = context.testCase.getTestStepAt(0).testRequest.response.getResponseContent()
assert response == '"48d45ba2-4549-41be-8b31-e85b3c738a03"'
stripResponse = response.replace("\"", "")
assert stripResponse == '48d45ba2-4549-41be-8b31-e85b3c738a03'
The stripResponse is the response where the quotation marks are stripped away. You could then proceed to assign this variable to a property of choice, or inject it directly in the headers of a next step.
Sometimes scripting is the way to go.

Rails UTF-8 response

I've got a Rails 3.2 app running on Ruby 1.9.3 that returns JSON data stored in a MongoDB database. The data seems to be stored correctly in mongo, e.g. (look at the name attribute):
{ "_id" : ObjectId("4f986cbe4c8086fdc9000002"), "created_at" : ISODate("2012-04-25T21:31:45.474Z"), "updated_at" : ISODate("2012-04-26T22:07:23.901Z"), "creator_id" : ObjectId("4f6b4d3c4c80864381000001"), "updater_id" : null, "name" : "Trädgår'n", "sort" : "tradgarn", "address" : "Nya Allén 11", "coordinates" : [ 11.9764791, 57.7045625 ], "phone" : "46031102080", "url" : "http://www.profilrestauranger.se/tradgarn/", "user_ids" : [ ] }
But when I issue a request that returns this record, I get something like this back (now look at the name attribute):
{"address":"Nya All\u00e9n 11","coordinates":[11.9764791,57.7045625],"created_at":"2012-04-25T23:31:45+02:00","id":"4f986cbe4c8086fdc9000002","name":"Tr\u00e4dg\u00e5r'n","phone":"46031102080","sort":"tradgarn","updated_at":"2012-04-27T00:07:23+02:00","url":"http://www.profilrestauranger.se/tradgarn/"}
The response headers for anyone interested:
HTTP/1.1 200 OK
Server: nginx/1.2.0
Date: Thu, 26 Apr 2012 22:41:13 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 909
Connection: keep-alive
Status: 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Max-Age: 1000
Access-Control-Allow-Headers: *,x-requested-with
X-UA-Compatible: IE=Edge
ETag: "d2a95f06bec10d8087c3188280292d3c"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: fdd042568195df279e59affe45bdcd37
X-Runtime: 0.037134
I cannot seem to figure out why or where the encoding is getting messed up? What gives? Help? :)
The issue is indeed one of JSON UTF-8 encoding. The #to_json method is escaping unicode characters. This can be observed by something like:
user.to_json
# => "{\"created_at\":\"2012-04-19T18:48:01Z\",\"email\":\"tr\\u00e4dg\\u00e5r#example.com\",\"id\":10,\"updated_at\":\"2012-04-27T18:37:10Z\"}"
When parsed, however, this is converted back to how you would expect it. It is possible, however, to generate the JSON using JSON.generate, with which the #as_json method can be used, along with any options for the construction. This doesn't escape the unicode. To do such:
JSON.generate(user.as_json)
# => "{\"created_at\":\"2012-04-19T18:48:01Z\",\"email\":\"trädgår#example.com\",\"id\":10,\"updated_at\":\"2012-04-27T18:37:10Z\"}"
Turns out the problem I was seeing was with the gem colorful_json. I was running the JSON thru its CLI utility cjson, and it was messing up the Unicode. I reported the issue and the new version of the gem fixes this.

Fiddler add binary file data to POST

I'm try to add binary file data directly to the request body of a POST call so I can simulate a file upload. However, I tried setting a 'before request' breakpoint and using 'insert file' but I couldn't seem to get that to work. I also tried to modify CustomRules.js to inject the file but couldn't figure out how to load binary data via JScript. Is there an easy solution here?
I'm sure this is a new feature in the year since this question was answered, but thought I'd add it anyhow:
There's a blue "[Upload file]" link in Composer now on the right side under the URL textbox. This will create a full multipart/form-data request. If you use this, you'll notice in the body you now have something that looks like this:
<#INCLUDE C:\Some\Path\my-image.jpg#>
In my case, I just wanted to POST the binary file directly with no multipart junk, so I just put the <#INCLUDE ... #> magic in the request body, and that sends the binary file as the body.
In order to send multipart/form-data, this receipe will be helped.
In upper panel (Http header), set Content-Type as below. Other values are automatically resolved.
Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468
And, input Response Body at the below panel as follows.
---------------------------acebdf13572468
Content-Disposition: form-data; name="description"
the_text_is_here
---------------------------acebdf13572468
Content-Disposition: form-data; name="file"; filename="123.jpg"
Content-Type: image/jpg
<#INCLUDE *C:\Users\Me\Pictures\95111c18-e969-440c-81bf-2579f29b3564.jpg*#>
---------------------------acebdf13572468--
The import rules are,
Content-Type should have two more - signs than boundary words in body.
The last of the body should be ended with two - signs.
In Fiddler script: (in Fiddler: Rules... Customize Rules), find the OnBeforeRequest function, and add a line similar to:
if (oSession.uriContains("yourdomain"))
{
oSession.LoadRequestBodyFromFile("c:\\temp\\binarycontent.dat");
}
since version 2.0, the Request Body has an "Upload File..." link that allows you to post/upload binary data.

How can i import a https webservice (wcf) with Delphi 2010?

I have a problem so i wanna help from you.
There is a webservice and it's url : https://kps.saglik.gov.tr/Services/KPSTestServices.svc
We can import this url on visual studio c# easily, but we couldn't import it on delphi 2010.
Also we have a user id and password from saglik.gov.tr, but the result is same : can't import this service. What can we do now ?
Thanks for any idea from at the moment.
Visual Studio Sample : https://kps.saglik.gov.tr/kps_ornek1.zip
I was able to import files to your local hard disk recording.
https://kps.saglik.gov.tr/Services/KPSTestServices.svc?wsdl = services.wsdl
https://kps.saglik.gov.tr/Services/KPSTestServices.svc?wsdl=wsdl0 = secondry.wsdl
https://kps.saglik.gov.tr/Services/KPSTestServices.svc?xsd=xsd0 = xsd0.xsd
https://kps.saglik.gov.tr/Services/KPSTestServices.svc?xsd=xsd1 = xsd1.xsd
https://kps.saglik.gov.tr/Services/KPSTestServices.svc?xsd=xsd2 = xsd2.xsd
When I call service , Now I get the https connection failed error.
Xml created by the WCFStorm
(http://www.wcfstorm.com/wcf/home.aspx)
Header
POST https://kps.saglik.gov.tr/Services/KPSTestServices.svc HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8
Host: kps.saglik.gov.tr
Content-Length: 1256
Expect: 100-continue
Connection: Keep-Alive
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><s:Header><a:Action s:mustUnderstand="1">http://www.saglik.gov.tr/KPS/2011/KPSServices/TcKimlikNoIleKisiSorgula</a:Action><a:MessageID>urn:uuid:b99c1ef4-9e44-42b9-ae1f-9844bfc0f997</a:MessageID><a:ReplyTo><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo><a:To s:mustUnderstand="1">https://kps.saglik.gov.tr/Services/KPSTestServices.svc</a:To><o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><u:Timestamp u:Id="_0"><u:Created>2011-09-13T11:17:27.234Z</u:Created><u:Expires>2011-09-13T11:22:27.234Z</u:Expires></u:Timestamp><o:UsernameToken u:Id="uuid-d7a725d3-aa3c-4336-8651-304ecdb8d5e0-4"><o:Username>myUserName</o:Username><o:Password o:Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">myPassword</o:Password></o:UsernameToken></o:Security></s:Header><s:Body><TcKimlikNoIleKisiSorgula xmlns="http://www.saglik.gov.tr/KPS/2011"><tcNo>1842715****</tcNo></TcKimlikNoIleKisiSorgula></s:Body></s:Envelope>
Response XML
Header
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 1519
Content-Type: application/soap+xml; charset=utf-8
Server: Microsoft-IIS/7.5
Set-Cookie: ASP.NET_SessionId=0vdbbkozgztz4falycku4gr0; path=/; HttpOnly
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 13 Sep 2011 11:19:40 GMT
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><s:Header><a:Action s:mustUnderstand="1">http://www.saglik.gov.tr/KPS/2011/KPSServices/TcKimlikNoIleKisiSorgulaResponse</a:Action><a:RelatesTo>urn:uuid:b99c1ef4-9e44-42b9-ae1f-9844bfc0f997</a:RelatesTo><o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><u:Timestamp u:Id="_0"><u:Created>2011-09-13T11:19:41.255Z</u:Created><u:Expires>2011-09-13T11:24:41.255Z</u:Expires></u:Timestamp></o:Security></s:Header><s:Body><TcKimlikNoIleKisiSorgulaResponse xmlns="http://www.saglik.gov.tr/KPS/2011"><TcKimlikNoIleKisiSorgulaResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Hata i:nil="true"/><Sonuc><Ad>MUSTAFA</Ad><AileSiraNo>12</AileSiraNo><AnaAd>LEYLA</AnaAd><BabaAd>OSMAN</BabaAd><BireySiraNo>*</BireySiraNo><CiltAd>-</CiltAd><CiltKod>-</CiltKod><Cinsiyet>Erkek</Cinsiyet><Din i:nil="true"/><DogumTarihi>21.9.0000</DogumTarihi><DogumYer>*</DogumYer><Durum>Açık</Durum><Hata i:nil="true"/><IlAd>*</IlAd><IlKod>*</IlKod><IlceAd>Abana</IlceAd><IlceKod>*</IlceKod><MedeniHal>Bekar</MedeniHal><OlumTarih>0.0.0</OlumTarih><Soyad>*</Soyad><TCKimlikNo>18427***</TCKimlikNo><Yakinlik>Kendisi</Yakinlik></Sonuc></TcKimlikNoIleKisiSorgulaResult></TcKimlikNoIleKisiSorgulaResponse></s:Body></s:Envelope>
Delphi 2010
My Request
CONNECT kps.saglik.gov.tr:443 HTTP/1.0
User-Agent: Borland SOAP 1.2
Host: kps.saglik.gov.tr:443
Content-Length: 0
Proxy-Connection: Keep-Alive
Pragma: no-cache
No XML
My Response
Header
HTTP/1.1 502 Unable to Secure Connection
Via: 1.1 SYSTMG07
Connection: Keep-Alive
Proxy-Connection: Keep-Alive
No XML
You need to point delphi towards the WSDL of the service. just append ?wsdl to the end of the first URL you gave: https://kps.saglik.gov.tr/Services/KPSTestServices.svc?wsdl and open this URL.
Save the file as a wsdl file and import it using: File -> New -> Other -> Delphi Projects -> Webservices -> WSDL Importer.
Please note: when the webservice changes you need to reimport. Also, using the webservice this way, you might need to deploy the wsdl with your application, depending on wether or not you are using the wsdl when consuming the webservice.
Maybe I am a little bit pessimistic about Delphi SOAP support, but I think this will be easier to solve using a "proxy" service written in C# (or Java, depending on available developer resources).
I have used a 'proxy' solution succesfully for the integration of a Microsoft Dynamics CRM system. After many attempts to solve it in Delphi, we finally wrote a simple C# application which reads input data from files (created from the Delphi app) and then invokes the web service operations using the data from these files.
Fortunately this was only a very simple one-way interface. For advanced requirements, the Delphi app could communicate over a TCP based protocol (HTTP) using XML or JSON payload. For bidirectional data exchange, the Delphi application would have to run a HTTP server too, which can be implemented with Indy in a couple of code lines. For bidirectional operation, using a messaging middleware like MSMQ (or one of the many open source message brokers like ActiveMQ or OpenMQ) can also be a good solution, because all applications can go offline for maintenance without the danger of message loss.
I just went through a similar problem attempting to connect a D2007 client to a C# service. The details I found were interesting. First of all the Delphi importer seriously under performed. I switched to RemObj and continued to have problems. At the end of the day there seems to be 3 items of interest when making the call to a WCF C# web service from a Delphi App. Those 3 items are as follows...
1) Make sure on the Delphi side you have the Soap version set to 1.2 (this is the easy one)
2) The post command being generated was including an incorrect action.
needed: POST "http://URI/Service/Command"
RemObj: POST urn:HCSConnect-WSHCSConnect#Ping
Delphi: ---post command missing completely---
What I did was change some details in the Rem Obj stuff and make it look like what I needed
3) With the service I was using the soap envelope wants a header area that contains a definition of a few details. In my situation the most important items in this header turned out to be the "a:Action" and "a:To" tags. These tags are missing in the Delphi apps, RemObj or otherwise. So in my situations I am intercepting the xml and filling it in.
The solutions to the last 2 items are not so simple... if you are using Rem Obj you can read about how I fixed it and what you can do the RemObj forum.
http://connect.remobjects.com/discussion/825/problem-calling-web-service-built-with-c
.... last but not least ...
I also got the thing to work using direct calls... if you are not using RemObj and Delphi is being a pain you can always just build your post and xml yourself and send it. Just like I did below... remember, post/xml/web it's all just plain text being sent back and forth.
procedure TMainForm.Button5Click(Sender: TObject);
procedure HandleError(const errorCode: integer);
var
errorMessage: AnsiString;
begin
SetLength(errorMessage, 256);
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_FROM_HMODULE,
Pointer(GetModuleHandle('wininet.dll')),
errorCode, 0, PChar(errorMessage), Length(errorMessage), nil);
SetLength(errorMessage, StrLen(PChar(errorMessage)));
raise Exception.Create(errorMessage);
end;
function BuildHeader: TStringStream;
begin
result := TStringStream.Create('');
try
result.WriteString('Content-Type: application/soap+xml;charset=UTF-8;action="http://Thermo.Connect/IHCSConnect/Ping"' + sLineBreak);
except
result.Free;
raise;
end;
end;
function BuildBody: TStringStream;
begin
result := TStringStream.Create('');
with result do
try
WriteString('<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">' + sLineBreak);
WriteString('<s:Header>' + sLineBreak);
WriteString('<a:Action>http://Thermo.Connect/IHCSConnect/Ping</a:Action>' + sLineBreak);
WriteString('<a:To>http://thermo-pc:2021/WSHCSConnect</a:To>' + sLineBreak);
WriteString('</s:Header>' + sLineBreak);
WriteString('<s:Body>' + sLineBreak);
WriteString('<Ping xmlns="http://Thermo.Connect">' + sLineBreak);
WriteString('</Ping>' + sLineBreak);
WriteString('</s:Body>' + sLineBreak);
WriteString('</s:Envelope>' + sLineBreak);
except;
result.Free;
raise;
end;
end;
var
InetRoot: HINTERNET;
InetConnect: HINTERNET;
Request: HINTERNET;
begin
InetRoot := InternetOpen('GabeCode', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
InetConnect := InternetConnect( InetRoot, 'thermo-pc:2021', 0, '',
'', INTERNET_SERVICE_HTTP, 0, Cardinal(Self));
try
Request := HttpOpenRequest( InetConnect, 'POST', 'WSHCSConnect', 'HTTP/1.1', nil, nil,
INTERNET_FLAG_KEEP_CONNECTION or INTERNET_FLAG_NO_CACHE_WRITE,
0);
try
// build add header items to the post request
with BuildHeader do
try
HttpAddRequestHeaders(Request, PChar(DataString), Length(DataString), HTTP_ADDREQ_FLAG_ADD);
finally
Free;
end;
// build the body of data being posted and send the post
with BuildBody do
try
if not HttpSendRequest(Request, nil, 0, PChar(DataString), Length(DataString)) then
HandleError(GetLastError);
finally
Free;
end;
finally
InternetCloseHandle(Request);
end;
finally
InternetCloseHandle(InetConnect);
end;
finally
InternetCloseHandle(InetRoot);
end;
end;