1

I got a problem that is driving me crazy. Hopefully you guys can help:

I am trying to integrate the eKomi Api.

For this I am doing a callout which returns the response System.HttpResponse[Status=OK, StatusCode=200]. So everything seems fine.

However, when I try to debug the body via getBody(), I get the following error message:

FATAL_ERROR|System.CalloutException: Could not parse HTTP response body to string

When I enter the callout in my browser, I receive the following response:

{"link":"https:\/\/www.ekomi.de\/kundenmeinung.php?kunde=customerId&shop=shopId","hash":"hash","known_since":"2017-07-24","done":1,"done_at":1500974822}

When I try to debug this String, I get the following error message upon deploy:

Validation Failed ERROR validating ApexClass classes/SendEkomiReviewRequest.cls: Invalid string literal '{"link":"https://www.ekomi.de/kundenmeinung.php?kunde=customerId&shop=shopId","hash":"hash","known_since":"2017-07-24","done":1,"done_at":1500974822}'. Illegal character sequence '/' in string literal.

The problem is that I can't change what's in the response. As the validation of the string fails, I can neither remove nor replace the illegal character sequence.

Do you have any idea what to do here? I'm desperate for any ideas.

Thanks a lot Daniel

  • Since the response you received is in JSON, you need to deserialize it. Check here developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – Animesh Jul 25 '17 at 10:16
  • Thanks. I tried that but it doesn't work either because the deserialization method also requires the body as a String. When I try to pass the response body to the deserialize(jsonString, apexType) method I get the same error message as when I try to debug it ("Could not parse HTTP response body to string"). – Daniel E. Jul 25 '17 at 12:33
  • Have you tried going via a Blob? getBodyAsBlob().toString() No idea if that will actually work but you have a pretty odd case here! – Aidan Jul 25 '17 at 12:46
  • Wow, for some reason that did the trick! Thank you!! Do you have any idea why this might have worked? I debugged the result of the getBodyAsBlob().toString() and it's the very same string as before. But for some reason I don't get a verification error. But when I take this resulting string and debug it again, I get the error message again! Very weird behaviour in my opinion. – Daniel E. Jul 25 '17 at 13:02
1

Have you tried going via a Blob?

getBodyAsBlob().toString() 

No idea if that will actually work but you have a pretty odd case here!

(Turns out that this worked, so submitted as an answer instead of a comment)

I don't know why it works, I'd guess that SF is trying to parse the raw http response in the getBody() version and getting it wrong. Or perhaps the API is returning something that isn't quite valid. Not sure, really.

  • Seems to be another peculiarity of Apex. In a string, the backslash \ appears to only be valid when followed by 'n' or 'r' (i.e. the escape sequences for newline and carriage return). Anything else requires a second backslash for Apex to not complain about it (this also applies to regex strings in Apex, which makes them even more 'fun' than usual). As far as I can tell, using Blob doesn't cause Apex to perform any string processing, and toString() creates a valid string through some magic. – Derek F Jul 25 '17 at 13:30
  • Through additional testing \' and unicode sequences like \u1234 are also valid. My best guess is that this is behavior that stems from Java. Or, rather, that it's a consequence of being able to put escape sequences into strings. As to why OP can't debug the blob's string representation after its been stored in a string variable, I have no idea. – Derek F Jul 25 '17 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.