scalaxb.toXML and XML character reference - scala

I use scalaxb.toXML to parse an object to xml and send it using soap.
When I have XML character reference in some string property, scalaxb.toXML encodes even the XML character.
How to avoid the encode of XML characters? Or escape it?
My string: Cancellation Fees
- Until 30 days before departure: 20% of the product price
My result (cleaned up):
<soap11:Envelope xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" ...>
...
<mbm:order-remark text="Cancellation Fees &#xd;&#xa;&#xd;&#xa; - Until 30 days before departure: 20% of the product price" position="0" />
...
</soap11:Envelope>
Each & is being encoded to & to avoid structural problems with my XML, even when they are valid.
How I call scalaxb.toXML:
scalaxb.toXML(obj, namespace, elementLabel, scope)

Related

Requests fail authorization when query string contains certain characters

I'm making requests to Twitter, using the OAuth1.0 signing process to set the Authorization header. They explain it step-by-step here, which I've followed. It all works, most of the time.
Authorization fails whenever special characters are sent without percent encoding in the query component of the request. For example, ?status=hello%20world! fails, but ?status=hello%20world%21 succeeds. But the change from ! to the percent encoded form %21 is only made in the URL, after the signature is generated.
So I'm confused as to why this fails, because AFAIK that's a legally encoded query string. Only the raw strings ("status", "hello world!") are used for signature generation, and I'd assume the server would remove any percent encoding from the query params and generate its own signature for comparison.
When it comes to building the URL, I let URLComponents do the work, so I don't add percent encoding manually, ex.
var urlComps = URLComponents()
urlComps.scheme = "https"
urlComps.host = host
urlComps.path = path
urlComps.queryItems = [URLQueryItem(key: "status", value: "hello world!")]
urlComps.percentEncodedQuery // "status=hello%20world!"
I wanted to see how Postman handled the same request. I selected OAuth1.0 as the Auth type and plugged in the same credentials. The request succeeded. I checked the Postman console and saw ?status=hello%20world%21; it was percent encoding the !. I updated Postman, because a nice little prompt asked me to. Then I tried the same request; now it was getting an authorization failure, and I saw ?status=hello%20world! in the console; the ! was no longer being percent encoded.
I'm wondering who is at fault here. Perhaps Postman and I are making the same mistake. Perhaps it's with Twitter. Or perhaps there's some proxy along the way that idk, double encodes my !.
The OAuth1.0 spec says this, which I believe is in the context of both client (taking a request that's ready to go and signing it before it's sent), and server (for generating another signature to compare against the one received):
The parameters from the following sources are collected into a
single list of name/value pairs:
The query component of the HTTP request URI as defined by
[RFC3986], Section 3.4. The query component is parsed into a list
of name/value pairs by treating it as an
"application/x-www-form-urlencoded" string, separating the names
and values and decoding them as defined by
[W3C.REC-html40-19980424], Section 17.13.4.
That last reference, here, outlines the encoding for application/x-www-form-urlencoded, and says that space characters should be replaced with +, non-alphanumeric characters should be percent encoded, name separated from value by =, and pairs separated by &.
So, the OAuth1.0 spec says that the query string of the URL needs to be decoded as defined by application/x-www-form-urlencoded. Does that mean that our query string needs to be encoded this way too?
It seems to me, if a request is to be signed using OAuth1.0, the query component of the URL that gets sent must be encoded in a way that is different to what it would normally be encoded in? That's a pretty significant detail if you ask me. And I haven't seen it explicitly mentioned, even in Twitter's documentation. And evidently the folks at Postman overlooked it too? Unless I'm not supposed to be using URLComponents to build a URL, but that's what it's for, no? Have I understood this correctly?
Note: ?status=hello+world%21 succeeds; it tweets "hello world!"
I ran into a similar issue.
put the status in post body, not query string.
Percent-encoding:
private encode(str: string) {
// encodeURIComponent() escapes all characters except: A-Z a-z 0-9 - _ . ! ~ * " ( )
// RFC 3986 section 2.3 Unreserved Characters (January 2005): A-Z a-z 0-9 - _ . ~
return encodeURIComponent(str)
.replace(/[!'()*]/g, c => "%" + c.charCodeAt(0).toString(16).toUpperCase());
}

Must HTTP Status Codes be strings?

The Responses object contains a {HTTP Status Code: Response} mapping.
In all the examples I found, the status code is provided as a string:
{"200": {"description": "a pet to be returned"}}
I couldn't find any requirement for it to be a string and integers are accepted by the validators I tried.
All I found was a PR changing from integer to string in all YAML examples.
Should I only use strings?
Edit: In JSON, only strings are valid keys. So the question could be rephrased as "which of the following two assumptions is correct"?
OpenAPI doesn't specify that HTTP Status Codes should be strings because that's implicit (JSON format). However, validation and display tools are being loose about that requirement.
OpenAPI uses some kind of "JSON superset" in which integer keys are considered valid.
From this GH issue, the keys must be strings:
OpenAPI can be represented canonically in either JSON or YAML, as you say in JSON only strings can be keys. With regard to YAML:
This field MUST be enclosed in quotation marks (for example, "200") for compatibility between JSON and YAML.
This has the effect that the key is always a string type.
This is not really a specification, but rather a requirement of the JSON format.

Can Integer in URL be typecasted to String in Query Param?

For the request
http://xyz/resource?articleid=232&name=John
Response getDetails(#QueryParam("articleid") String articleid,(#QueryParam("name") String name){}
Is the above Query Parameter correct for the given URL?
Executive summary: there is no "integer" in the URL; there's only a string. The implementation is does extra work to convert the string into an integer if you ask it to do so.
Is the above Query Parameter correct for the given URL?
That should be perfectly acceptable.
https://www.rfc-editor.org/rfc/rfc3986#section-3.4
query is a just a sequence of pchar (plus '/' and '?'), which is to say it's just data.
A query in that form is usually an expression of an application/x-www-form-urlencoded resource. The key hint in the specification is
Let output be an initially empty list of name-value tuples where both name and value hold a string.
The JAX-RS specification describes the transformation of these string to other types, but it defers to the java doc for the annotation. Of course, QueryParam is in close alignment with the specification, so both places give the same answer.

In Jmeter how to pass Date as String/text

I need to pass a date (12/12/2016) as a Text parameter to a Post request.
It picks %2f instead of '/'. how do i pass this as string with date having slash.
I tried to send date value as Text/string in parameters tab. whether I select/unselect Encode? parameter, sending by encoding the date value.
Keeping the value in Body Data section worked for me.
So, try by moving all the Post data into Body Data section instead of Parameters section.

Wrong NSURLQueryItem percentage encoding for Google CSE

I'm writing app using Google custom search engine.
I received my search engine ID XXXXXXXX219143826571:7h9XXXXXXX (most interesting part bold).
Now I'm trying to use NSURLQueryItem to embed my ID into URL by using:
let params = ["cx" : engineID,...]
...
components.queryItems = parameters.map {
NSURLQueryItem(name: String($0), value: String($1))
}
It should percentage escape item to XXXXXXXX219143826571%3A7h9XXXXXXX (This value I'm getting when using Google APIs explorer while testing, it shows url dress that was used). It is not doing it. I'm getting url without escaping, no changes. If I use escaped string as engine ID in this mapping, I'm getting escaped string XXXXXXXX219143826571%253A7h9XXXXXXX (additional '25' is added to query).
Can someone tell me how to fix it? I don't want to use String and then convert it to URL by NSURL(string: str)!. It is not elegant.
Edit:
I'm using app Info.plist to save ID and I retrieve it by calling:
String(NSBundle.mainBundle().objectForInfoDictionaryKey("ApiKey")!)
Colons are allows in the query part of a URL string. There should be no need to escape them.
Strictly speaking, the only things that absolutely have to be encoded in that part of a URL are ampersands, hash marks (#), and (assuming you're doing a GET query with form encoding) equals signs. However, question marks in theory may cause problems, slashes are technically not allowed (but work just fine), and semicolons are technically allowed (but again, work in practice).
Colons, AFAIK, only have special meaning in the context of paths (if the OS treats it as a path separator) and in that it separates the scheme (protocol) from the rest of the URL.
So don't worry about the colon being unencoded unless the Google API barfs for some reason.