Im sending (with ASIFormDataRequest) a POST request, with 4 parameters. The server launches a NullPointerException when this request arrives. When I debug the app, I don't know how to actually see what's inside the parameters in the request done by the phone. Is there any way to achieve this? Maybe I am missing some option in XCode?
Thank you.
To see the post parameters, you will need to look inside yourFormRequest.postBody, but please note the data is NSMutableData so you will have to convert it to NSString or NSDictionary
Like following
NSString *str = [[NSString alloc] initWithData:[yourFormRequest.postBody] encoding:NSUTF8StringEncoding];
Related
I am integrating PayTm with my app and I want to pass the parameters using GET method.
My code is as follows:
NSString *urlString = [NSString stringWithFormat:#"https://secure.paytm.in/oltp/HANDLER_INTERNAL/TXNSTATUS?JsonData={%22MID%22:%22%#%22,%22ORDERID%22:%22a84afd6c-0e54-42df-b29a-2b057f9e7c53%22}",MIDValue];
where MIDValue is a string.
When I use this code I'm getting error message.
Please give a suggestion to remove the error.
Thank You
Maybe your variable MIDValue contains space and/or &.
You first have to encode it as URL and then pass it as parameter.
Visit this for details, I guess this is what you are looking for
NSString *newParam = [MIDValue stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]
NSString *urlString = [NSString stringWithFormat:#"https://secure.paytm.in/oltp/HANDLER_INTERNAL/TXNSTATUS?JsonData={%22MID%22:%22%#%22,%22ORDERID%22:%22a84afd6c-0e54-42df-b29a-2b057f9e7c53%22}", newParam];
Yet another problem with Twilio for me. So, I'm trying to connect with another user with parametes. Here's my code:
-(void)connect:(CDVInvokedUrlCommand*)command {
NSLog(#"The content of dict is%#",[command.arguments objectAtIndex:0]);
NSDictionary *dict = [command.arguments objectAtIndex:0];
for(NSString *key in [dict allKeys]) {
NSLog(#"%#",key);
NSLog(#"%#",[dict objectForKey:key]);
}
[self.device connect:dict delegate:self];
}
Problem: no matter what I have in NSDictionary nothing gets passed to server. I have tryied with number of parameters, nothing gets passed. Not even To parameters, on non of other, custom ones. It works for every other platform, but not for iOS.
What em I missing here? According to docs it should work, connect method is taking NSDictionary as input,
-connect:(NSDictionary*)params delegate:(id<TCConnectionDelegate>)delegate, so where should I look?
PS. This is yet another big problem Twilio lib that I had in last few days. Im getting enough of it.
OK, the problem was that I was passing NSDictionary with key that had integer value. The -connect method accepts only <NSString NSString> NSDictionary.
Let's figure this out. It’s not entirely clear from your code sample what exactly you've passed in as arguments so forgive me if I suggest something you might've already tried.
What happens if you try something like this?
NSDictionary *params = #{#"To": "number_to_dial"};
_connection = [_phone connect:params delegate:self];
If this does not work, we can be certain the problem lies in the account, token, or app itself.
The iOS sample shows how to pass the dialing parameters, however it’s still the developer’s responsibility to create proper TwiML that contains the dialing command tags that Twilio recognizes.
If the call is hitting your server though without any parameters being passed in, there should be a Twilio CallSid available in the TCConnectionDelegate.connectionDidConnect: method. If that's the case, I would suggest contacting support with that Sid and we can dig more info from the server to see what parameters are being passed.
Here's how to retrieve the CallSid:
- (void)connectionDidConnect:(TCConnection *)connection {
NSString *callsid = [[connection parameters] objectForKey:#"CallSid"];
...
}
Could you see if you are able to see this callback method being called and try to provide the CallSid if possible?
I know this question is asked before, but none of the solutions worked for me. I am trying to convert an NSData object to a NSString value. I am initing the NSString object like following:
NSString *html = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
But the html is always nil. The NSData I am trying to convert is the source code of a website. It is fairly long. This is 'NSData` I am trying to convert.
Is it the length of the data that is causing the issue? I need the source code as a string. What can I do to resolve this issue?
What I tried so far:
Tried with all encoding formats as shown in this answer.
Tried with [NSString stringWithUTF8String:[urlData bytes]];
But whatever I do produce the same result. html always is nil whatever I do.
EDIT
It was a problem with the debug console. Even when the objects had values in it, the debug console always showed nil as the value for most of the objects. However NSLog always displays the value.
It's not a problem with debugger
The problem comes from compiler optimization, compiler see that string was not directly used, and optimizes the code by removing it and directly passing it to another method.
The key of the problem : You are running project on release scheme
Solution:
Here is a small guide to switch project to the Debug scheme
1) Click on the target, and click Edit scheme...
2) Popup will be displayed
3) Click Run %Your project%
4) Open Build Configuration popup
5) Select Debug
5) Press OK
6) You are ready to Go!, now you can debug anything :)
If you are using ARC, and you just wrote the code that converts the data to a string and haven't written any code yet that actually uses the string, it will get deallocated immediately. Check whether that is what is happening. For example, what does NSLog (#"%#", html) display?
NSAttributedString *str = [[NSAttributedString alloc] initWithData:data options:#{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]}
documentAttributes:nil error:&error];
Try this one:
NSString *myString = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
Generally, conversion from NSData to NSString returns nil means there is mismatch between encoding format received from server and approach used for encoding.
Xcode 5, I'm debugging and trying to see the value of the HTTPBody that's being POSTed to an API. The API isn't registering the POST so I need to debug and see if the content of it is correct.
Here's the line of code:
request.HTTPBody = [report multiPartFormDataWithBoundary:boundary];
Debugging into the multiPartFormDataWithBoundary method I can see the form POST elements being built up but when it comes out of that method and is assigned as above I can no longer see the contents in the debugger.
How can I check what is being sent to the API?
Since HTTPBody is a NSData type, you can convert to a NSString and show it by NSLog.
NSLog(#"%#", [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding]);
Let me start off by saying that I am not particularly trying to find a solution, just the root cause of the problem. I am trying to retrieve a JSON from a url. In browser, the url call works just fine and I am able to see the entire JSON without issue. However, in x-code when simply using NSURLConnection, I am getting data bytes, but my NSString is null.
theString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
After doing some research I have found that I am probably trying to use the wrong encoding. I am not sure what type of encoding is being used by the url, so on first instinct I just tried some random encoding types.
NSString* myString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSString* myString2 = [[NSString alloc] initWithData:data encoding:NSUTF16StringEncoding];
NSString* myString3 = [[NSString alloc] initWithData:data encoding:NSWindowsCP1252StringEncoding];
NSASCIIStringEncoding and NSWindowsCP1252StringEncoding is able to bring back a partially correct JSON. It is not the entire JSON thatI am able to view in the browser, and some characters are a little messed up, but it is something. To try and better determine what encoding was used, I decided to use the following method to try and determine it by looking at what encoding returned.
NSError *error = nil;
NSStringEncoding encoding;
NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];
My NSStringEncoding value is 3221214344. And this number is consistent everytime I run the app. I can not find any NSStringEncoding values that even come close to matching this.
My final question is: Is the encoding used for this url not consumable by iOS, is it possible that multiple types of encoding was used for this url, or is there something else that I could be doing wrong on my end?
It's best not to rely on Cocoa to figure out the string encoding if possible, especially if the data might be corrupted. A better approach would be to check if the value indicated by the HTTP Content-Type header specifies a character set like in this example:
Content-Type: text/html; charset=ISO-8859-4
Once you're able to parse and retrieve a character set name from the Content-Type header, you need to convert it to an NSStringEncoding, first by passing it to CFStringConvertIANACharSetNameToEncoding, and then passing the returned CF string encoding to CFStringConvertEncodingToNSStringEncoding. After that, you can initialize your string using -[NSString initWithData:encoding:].
NSData *HTTPResponseBody = …; // Get the HTTP response body
NSString *charSetName = …; // Get a charset name from the Content-Type HTTP header
// Get the Core Foundation string encoding
CFStringEncoding cfencoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)charSetName);
// Confirm this is a known encoding
if (cfencoding != kCFStringEncodingInvalidId) {
// Initialize the string
NSStringEncoding nsencoding = CFStringConvertEncodingToNSStringEncoding(cfencoding);
NSString *JSON = [[NSString alloc] initWithData: HTTPResponseBody
encoding: nsencoding];
}
You still may run into problems if the string data you're working with is corrupted. For example, in the above code snippet, perhaps charSetName is UTF-8, but HTTPResponseBody can't be parsed as UTF-8 because there's an invalid byte sequence. In this situation, Cocoa will return nil when you try to instantiate your string, and short of sanitizing the data so that it conforms to the reported string encoding (perhaps by stripping out invalid byte sequences), you may want to report an error back to the end user.
As a last-ditch effort — rather than reporting an error — you could initialize a string using an encoding that can handle anything you throw at it, such as NSMacOSRomanStringEncoding. The one caveat here is that unicode / corrupted data may show up intermittently as symbols or unexpected alphanumerics.
Even though it seems that the answer has been provided in the comments (using iso-8859-1 as the correct encoding) I thought it worthwhile to discuss how I would go about debugging this problem.
You said that the Desktop Browser (Chrome) can digest the data correctly, so let's use that:
Enable Developer Tools https://developers.google.com/chrome-developer-tools/
When the Dev Tools window is open, switch to "network" and execute your call in that browser tab
check the output by clicking on the request url - it should give you some clue.
If that doesn't work, tools like Postman can help you to recreate the call before you implement it on the device