JSON containing a regular expression in Objective C - objective-c

Here is a field value returned by web service in JSON ^\+?\d+(-\d+)*$ but when i get it, it becomes ^+?d+(-d+)*$.
But i need the original one from JSON. How to get the same from objective C.
For example : If i use NSString *string=#"^\+?\d+(-\d+)*$";,
It shows ^+?d+(-d+)*$ in NSLog. How to prevent this?
Thanks

You should add the escape character in the response. Get the response in below way from web service. Tell your web service developer to format the response in below way. Character '\' can be received via adding escape sequence.
^\\+?\\d+(-\\d+)*$

Related

Passing double equal sign in value in POSTMAN using raw text body

I'm using POSTMAN to send parameter to an API using raw text format. The data i sent has two equal sign in the end in one of the parameters. Lets say i have a variable a, b, and c. Variable a value is 1234567890==; so i write the full raw text as follow :
a=123456789==&b=1234&c=1234
but the postman didn't recognize the double equal sign at the end of variable a value, so it was like the POSTMAN does not recognized the double equal sign other than the separator between key and value. I tried using the x-form-www-urlencoded body it can send the double equal sign, but when i used raw text format it cannot. I'm required to use the raw text format to send the parameter to the API. How can i fix my problem.
Thank you
I just tried this using postman-echo and the data was sent properly. Check this sample request I've created for you.
As you can see I set the Body to the payload above, with raw text format:
Postman-echo replies exactly with the same payload, which means the server would have gotten the data as is{:

How to query an API greater than less than

Basic question but couldn't find an answer to this.
There is fake API testing tool located here
https://jsonplaceholder.typicode.com/todos
When I add a query like this
https://jsonplaceholder.typicode.com/todos?userId=4
I get a response
When I change the query to this
https://jsonplaceholder.typicode.com/todos?userId<4
it returns null
How would I query in the url userId that is less than 4?
You need to encode the '<' sign as a special character with its hexadecimal code ('<' is 3c) so your query becomes:
https://jsonplaceholder.typicode.com/todos?userId%3c4
Any special character in a url can be coded as % followed by its two-digit hexadecimal code.
EDIT: After trying the URL with my change, I was able to get the '<' sign decoded as I mentioned above. However, it doesn't seem to provide the expected return (all user IDs less than 4). It returns more user IDs than this. Maybe need to check the API docs to make sure that 'userId<4' is a correct field definition.

WCF Service With Restricted/Special Characters in Response

I have a WCF Service which returns a string response with &, <, >. For e.g.
<response>&</response>
Actually, I'm sending the '&' char but it is encoded for some reason.
Instead, I would like to send the decoded resoponse. The response I want is - <response>&</response>
Could someone suggest how to achieve this?
Thanks.
The XML Serializer has to encode special characters in order to generate a well-formed XML document.
According to the XML specification, the ampersand character must not appear in its literal form anywhere in the document, hence the encoding.
The consuming application should be aware of this already, and will know to decode the &amp ; back to ampersand. However, if you are looking at your response on a XML-based software such as Soap UI, you'll continue to see the symbol in its encoded form.
You can use
System.Web.HttpUtility.HtmlDecode()
on your perticular data contract property/entire response string. Or you can use method in below article that is not quite developer freindly but may be used if you have lots of special chars in your response.
http://seroter.wordpress.com/2007/11/09/xml-web-services-and-special-characters/

QueryString Encryption and Related Characters Problems

I'm using a 64base data encryption function to Encrypt and Decrypt emil addresses sent in links and back in QueryString using :
Encrypt(txtEmail.Text).ToString
// Which generate something like this " pqM/rgLD9PSrE+Ofm4pt4kg86+1RChHD "
Decrypt(Request("email").ToString
But the Decrypt didn't work fine and returned an error "Invalid length for a Base-64 char array" until I fond that I may solve it using :
Decrypt(Request("email").Replace(" ", "+").ToString)
Since the plus sign "+" character was generating a space when call from a URL.
I also tried UrlEncode but didn't help
Decrypt(Server.UrlEncode(Request("email")))
Now my questions is:
Is this the only problem may I face with the encrypted strings?
Is there another way to solve the problem more effective than I used with replace function?
Thank you all in advance
This would happen if you don't generate the URL properly.
The ASP.Net Request accessors will automatically decode the data that you access.
However, you need to URL-data-encode your string before putting it in the querystring in the first place.

IOS JSON escaping special characters

I'm working in IOS and trying to pass some content to a web server via an NSURLRequest. On the server I have a PHP script setup to accept the request string and convert it into an JSON object using the Zend_JSON framework. The issue I am having is whenever the character "ø" is in any part of the request parameters, then the request string is cut short by one character.
Request string before going to server.
[{"description":"Blah blah","type":"Russebuss","name":"Roscoe Simulator","appVersion":"1.0.20","osVersion":"IOS 5.1","phone":"5555555","country":"Østfold","udid":"bed164974ea0d436a43f3cdee0e005a1"}]
Request string on server before any parsing
[{"description":"Blah blah","type":"Russebuss","name":"Roscoe Simulator","appVersion":"1.0.20","osVersion":"IOS 5.1","phone":"5555555","country":"Nord-Trøndelag","udid":"bed164974ea0d436a43f3cdee0e005a1"}
Everything looks exactly the same except the final closing ] is missing. I'm thinking it's having an issue when converting the string to UTF-8, but not sure the correct way to fix this issue.
Does anyone have any ideas why this is happening?
first of all do not trust the xcode console in such cases. you never know which coding the console is actually using.
second, escape the invalid characters before you build you json string. easiest way would probably to make sure you are using the same unicode representation, like utf-8, all the time.
third, if there are still invalid characters use a json lib with a parser (does the encoding). validate the output by parsing back to e.g. NSString. or validate the output manually by using a web form like http://jsonformatter.curiousconcept.com/
the badest way is to replace the single characters in the string, build your json and convert back. one way to do this could be to replace e.g an german ä with its unicode representaion U+00E4 (http://www.utf8-chartable.de/).
Thats the way I do it. I am glad that I nerver needed to go further than step three and this is the step you should do anyway to keep your code simple.
Please try to use Zends internal json Encoding:
Zend_Json::$useBuiltinEncoderDecoder = true;
should fix your issue.