-1

Linkedin API Caption with Carriage Return

I'm using a carriage return on Linkedin Caption to share the post via Linkedin Share article API. But while sharing through API it returns an error like

"Error parsing request body to JSON Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using a backslash to be included in string value\n at [Source: (com.linkedin.data.ByteString$ByteArrayVectorInputStream)"

Is there any way to resolve this issue and pass a carriage return in the caption. Please help.

I have tried to replace the carriage return using "\n" since it shows like escape using backslash but it's not working for me.

$msg = 'test1 test2

    test3';

$msg = preg_replace("/[^A-Za-z ]/","\n",$msg);

I have tried the above but again I got the same error response from Linkedin API.

The expecting result to share a post with a caption like below,

"test1 test2

test3"

But it won't get a post to the user account instead it gives an error like

"Error parsing request body to JSON Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using a backslash to be included in string value\n at [Source: (com.linkedin.data.ByteString$ByteArrayVectorInputStream)"

Jainil
  • 1,488
  • 1
  • 21
  • 26
Ramya.K
  • 19
  • 5
  • Try putting an actual backslash-n in your string; when you write `"\n"`, php translates it to a newline and the API sees the same as before. (For example, write `"\\n"` in your code to insert real backslash.) – alexis Oct 21 '19 at 07:29
  • 2
    Show us your code where you are actually creating the JSON. Are you doing this manually? Then _stop that_, that is not a clever thing to do - create the appropriate _data structure_, and then use `json_encode` on it. – 04FS Oct 21 '19 at 07:31
  • @alexis Thank you so much. I have tried your trick and it's work perfectly. – Ramya.K Oct 21 '19 at 09:45
  • You are welcome. It's not a trick, it is regular PHP syntax for generating what the api asked for. I have written it out as an answer, please "accept" it to mark this question as resolved. (And then please do heed what @04FS wrote!) – alexis Oct 21 '19 at 10:53

1 Answers1

0

To insert a literal \n in a string, you need to escape the backslash; otherwise PHP understands the \n notation and will change it into an actual newline, which is what you are trying to avoid. So, write "\\n" instead of "\n" in your code snippet.

But the comment by @04FS is spot on: You should not be generating the json format yourself! Put your data in an array, without worrying about the escaping newlines and such, and use json_encode to serialize it.

alexis
  • 48,685
  • 16
  • 101
  • 161